GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 26-Apr-2006, 06:56
Fermat's Avatar
Fermat Fermat is offline
New Member
 
Join Date: Mar 2006
Location: North East of England
Posts: 23
Fermat is on a distinguished road

How do I reference an undeclared object ?


I have to write a program for a small spreadsheet. It's a simle one-dimensional spreadsheet with only one column (of figures, formulae).
The specification for the spreadsheet says that it should start out empty upon creation of the spreadsheet object, that there should be no cells.These cells are referenced in my source code, but since I can't declare an instance of of a cell, the compiler says that the cell object (actually a member element of it) has not been declared.
But if I do declare an instance, then I'm conflicting with the job spec.

How can I get around this?
Can I ?
How can I not create a cell for the spreadhseet yet still reference it in my source code without the compiler flagging an error ?
'Tis possible, even?

Any thoughts/comments would be appreciated.

BTW, does this post need any source code ??

I'm using DevC++ 4.9.9.2

Personally, I can't see how to get around it, So i've declared one instance of a cell, and am hoping the specification is wrong!!
  #2  
Old 26-Apr-2006, 11:27
MichaelS-R MichaelS-R is offline
Junior Member
 
Join Date: Apr 2006
Location: Berkshire, UK
Posts: 65
MichaelS-R is on a distinguished road

Re: How do I reference an undeclared object ?


I think some source code (object definitions) would be a help... and the code you have tried and are having trouble with.
  #3  
Old 26-Apr-2006, 11:54
Fermat's Avatar
Fermat Fermat is offline
New Member
 
Join Date: Mar 2006
Location: North East of England
Posts: 23
Fermat is on a distinguished road

Re: How do I reference an undeclared object ?


Here's some of my source code:
CPP / C++ / C Code:
struct ssCell {
       double value;
       string text;
       bool valid; // initialise to false;
};

struct ssCell* cell[100];
//struct ssCell cell[1];

class SpreadSheet {
      private:
              int N;    // number of cells in spreadsheet
      public:
             SpreadSheet();
             bool insertCell(int slotNum, const std::string& userText);
             bool updateCell(int slotNum, const std::string& userText);
             int getNumCells(void) {return N;}
             bool deleteCell(int slotNum);
             bool getCellText(int slotNum, const std::string& userText);
             bool getCellValue(int slotNum, double& value);
             void increaseCellNumbers(void) {N++;};
             void decreaseCellNumbers(void) {N--;};
             
             //struct ssCell* cell[MAXCELLS];
};
and the bit I'm having problems with,
CPP / C++ / C Code:
void addinCell(int row)
{double val; string s;
     struct ssCell* temp = new ssCell[row+1];
     int N = SS.getNumCells();
     for (int i=0; i < N; i++)
     {
         temp[i].value = cell[i].value;
         temp[i].text = cell[i].text;
     };
     for (int i=N; i <= row; i++)
     {
         temp[i].value = 0;
         temp[i].text = "0";
     };
     // cell[row] has just been created ans will be inserted with insertCell
     delete [] cell;
     cell = temp; // repoint p to the new/relocated instances of cell structs
}
// end addinCell
temp[n].value and temp[n].text are OK, but cell[n].value and cell[n].text get the error reports:
"In function `void addinCell(int)': `value' has not been declared "
"In function `void addinCell(int)': `text' has not been declared "
(I used n as the index in the lines above to avoid activating an italic tag)
  #4  
Old 26-Apr-2006, 12:12
Sokar Sokar is offline
Member
 
Join Date: May 2005
Posts: 243
Sokar has a spectacular aura aboutSokar has a spectacular aura about

Re: How do I reference an undeclared object ?


My guess would be that since "addinCell" is not a member function and has no "this" pointer. You are going to have to pass in "cell" or make "cell" have global scope.
  #5  
Old 26-Apr-2006, 12:55
Fermat's Avatar
Fermat Fermat is offline
New Member
 
Join Date: Mar 2006
Location: North East of England
Posts: 23
Fermat is on a distinguished road

Re: How do I reference an undeclared object ?


Thanks, but cell already is a global variable.
  #6  
Old 26-Apr-2006, 13:18
davis
 
Posts: n/a

Re: How do I reference an undeclared object ?


Quote:
Originally Posted by Fermat
Thanks, but cell already is a global variable.


CPP / C++ / C Code:

struct ssCell* cell[100];

         temp[i].value = cell[i].value;
         temp[i].text = cell[i].text;

...did you forget that you declared cell as a pointer type?

cell[i]->value
cell[i]->text


:davis:
  #7  
Old 26-Apr-2006, 17:10
Fermat's Avatar
Fermat Fermat is offline
New Member
 
Join Date: Mar 2006
Location: North East of England
Posts: 23
Fermat is on a distinguished road

Re: How do I reference an undeclared object ?


Hmm, Im not sure what's happening. I originally had

temp[n].value = cell[n]->value

and got an error with it. I had to change it to dot notation to get it to compile. BUt now when I have it in dot notation it doesn't compile any more, but it does if I go back to using arrows!! I must have changed something. So somehow or other it seems that my problem is solved

Thanks for your thoughts.
  #8  
Old 26-Apr-2006, 18:03
davis
 
Posts: n/a

Re: How do I reference an undeclared object ?


Quote:
Originally Posted by Fermat
Hmm, Im not sure what's happening. I originally had

temp[n].value = cell[n]->value

and got an error with it. I had to change it to dot notation to get it to compile. BUt now when I have it in dot notation it doesn't compile any more, but it does if I go back to using arrows!! I must have changed something. So somehow or other it seems that my problem is solved

Thanks for your thoughts.

CPP / C++ / C Code:
temp[n]->value = cell[n]->value

...you declared temp to be a pointer type, too!

struct ssCell* temp = new ssCell[row+1];


:davis:
  #9  
Old 26-Apr-2006, 23:54
Fermat's Avatar
Fermat Fermat is offline
New Member
 
Join Date: Mar 2006
Location: North East of England
Posts: 23
Fermat is on a distinguished road

Re: How do I reference an undeclared object ?


I've changed the code for that function, and it compiles OK. It's now
CPP / C++ / C Code:
void addinCells(int row)
{
     int N = SS.getNumCells();
     struct ssCell* temp = new ssCell[row+1-N];

     for (int i=N; i <= row; i++)
     {
         temp[i].value = 0;
         temp[i].text = "0";
         cell[i] = &temp[i];
     };
     // cell[row] has just been created and will be inserted with insertCell
}
// end addinCell
cell and temp are declared as,
CPP / C++ / C Code:
struct ssCell* cell[100];

struct ssCell* temp = new ssCell[row+1-N];
The only difference I can think of is that cell is static declaration and temp is dynamic. (Is that right?) But if I were to change the first line of the above code to
CPP / C++ / C Code:
temp[i].value = cell[i].value;
then I would get an error saying, " `value' has not been declared ", meaning cell's value, rather than temp's. The dot with cell has to be changed to an arrow,
CPP / C++ / C Code:
cell[i]->value;
to compile OK. And If I change the dot of temp to an arrow, I get the same " `value' not declared " error message.
So just what is the difference between cell and temp?

What I'm trying to do with the function is add on cells. I have N cells already existing and I want to increase that up to row+1 cells.
cell is supposed to be an array of pointers only, that then get to point at new instances of ssCell as they are (dynamically) created.

I know that's not what I've got. I'm still working on it
 
 

Recent GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
G++ question... TreyAU21 C++ Forum 10 24-Feb-2006 09:45
How to interpret characters as they are being entered? nkhambal C Programming Language 18 14-Feb-2006 11:41
c++ inheritance illbemissingu C++ Forum 14 23-Oct-2005 18:35
Can enum have same name as class? crystalattice C++ Forum 3 08-Dec-2004 17:43
fltk-2.0 cvs Plumb FLTK Forum 20 13-Nov-2004 08:10

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 22:44.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.