The main problem is with the classCPP.cpp file. I am trying to access the member variable "personName" of the object "john." (of class person) Since "john" is not declared in this cpp it gives me an error. Is there any way I can access members of object "john" in this cpp?
This is just an example of a sort of problem I am having with a big project. I am programming a game and got about 1000 lines of code in before I decided to break it all up into smaller pieces. Unfortunately after it was all broken up into individual cpp files. I have about 10 errors caused by these kinds of problems. Is there an easy way to make this work?
Code:
//classCPP.cpp
#include <iostream>
#include <string>
#include "classH.h"
using namespace std;
void vegetable::talk()
{
cout << john.personName << " has a " << vegetableName << endl; // possible to access object john?
}
Code:
//main.cpp
#include <iostream>
#include <string>
#include "classH.h"
using namespace std;
vegetable potato;
person john;
int main()
{
potato.vegetableName = "Potato";
john.personName = "John";
potato.talk();
system("PAUSE");
}
Code:
//classH.h
#ifndef CLASS_H
#define CLASS_H
#include <string>
#include <iostream>
using namespace std;
class vegetable;
class vegetable
{
public:
void vegetable::talk();
string vegetableName;
};
class person
{
public:
string personName;
};
#endif
16 C:\Dev-Cpp\classCPP.cpp `john' undeclared (first use this function)