error C2065: 'words' : undeclared identifier

Hi all,

Im new to c++, which i have been programming java for 6 years now and im still getting use to the syntax of this language. Im encounter an error with a nested class where for some reason it will not let me access a variable i declared in the second class. Here is the code below:


#include <iostream>
#include <fstream>
#include <string>
using namespace std;

class Hen
{
public:
void display();
~Hen();
string word;

class Nest
{
public:
Hen display();
~Nest();
string words;
};



};


int main()
{
Hen hen1;
Hen::Nest nest1;
hen1.display();
nest1.display();
hen1.~Hen();
nest1.~Nest();
cin.get();
return 0;

};

Hen::Nest display()
{
cout << "This is the Nest Constructor Please enter a word" << "\n";
cin >> words;
cout << "You Entered: " << words << "\n";
cin.ignore();

};

void Hen::display()
{
string word;
cout << "This is the Hen Constructor Please enter a word" << "\n";
cin >> word;
cout << "You Entered: " << word << "\n";
cin.ignore();
};




Hen::~Hen()
{
word = " ";
cout << "This is the deconstructor" << "\n";
};

Hen::Nest::~Nest()
{
words = " ";
cout << "This is the deconstructor" << "\n";
};
I donno the solution of your question but i strongly advise you to organize your code. Its so unreadable :(
You have a few problems:

1) Your error is because you're messing up the scope of your display function:

1
2
3
Hen::Nest display()  // <- this is wrong

Hen Hen::Nest::display() // <- it should be this 


Hen is the return type, Hen::Nest is the scope.


2) Your display function isn't returning anything. If you are telling it to return a Hen, then it should return a Hen. You are returning nothing, so you probably should change it to be a void function.

3) DO NOT MANUALLY CALL DESTRUCTORS. These are called automatically and you must NOT call them by hand or risk having objects destroyed multiple times, which is very very bad.

1
2
//hen1.~Hen();  // <- BAD!  don't do this!
//nest1.~Nest(); 
Last edited on
Thank You for fixing my problem. Going on number 2, All i want it to do is to display the method the text in that method. so if i return display(); it will go into an infinite loop. How would this be fixed if i just want it to call that method and display that msg.

As for 3 is there a way to display a msg during the deconstruction of the methods?
return values are for having function output. Not output to the user, but output to the program. You said you had 6 years Java experience so I figured you knew this already, but I'll go over it just in case...

For example, a function which sums two integers might return an integer:

1
2
3
4
5
6
7
8
9
int add(int a, int b)
{
  return a + b; // returns an int
}

int main()
{
  int sum = add(5,3);
}


In this example, 'add' returns the sum of a and b (which in this case are 5 and 3). Therefore 8 is returned, which is what gets assigned to the 'sum' variable.

So ask yourself this: What information does 'display' need to pass back to the program? I can't think of anything, really, so it makes sense for it to be a void function (ie: returns nothing).

 
void display();  // <- make it a void 




As for 3 is there a way to display a msg during the deconstruction of the methods?


Well the methods/functions don't destruct, the objects do. But yes, you simply have to pause the program with your cin.get() line after they are destroyed. You can do this by putting them in a smaller scope which will expire before cin.get() is executed.

One way to do this is to simply add another set of {braces}

1
2
3
4
5
6
7
8
int main()
{
  {
    Hen hen1;
  }  // hen1 is destroyed here, dtors message is printed

  cin.get();  // program paused here
}


Another way would be to put everything in a separate function:

1
2
3
4
5
6
7
8
9
10
void runprogram()
{
  Hen hen1;
} // hen1 destroyed here

int main()
{
  runprogram();
  cin.get();
}
Thanks Again, and I understand what you were getting at before, perfect i made the change for the deconstructor and it works wonderfully. Thanks again

Topic archived. No new replies allowed.