Originally Posted by
Dae
typical situation:
Code:
###### example.h
#ifndef _EXAMPLE_H_
#define _EXAMPLE_H_
int problem;
FruitClass RedFruit(); // error
FruitClass GreenFruit(); // error
#endif // _EXAMPLE_H_
###### example.cpp
problem = 5;
So the int is fine.. that works, but the class objects there will work but will return an error with multiple files (and the brackets dont matter so thats not it).
Anyone mind lending me ideas?
Thanks in advance,
1) You don't call default constructors like that.
2) It looks like you're trying to create some global variables for your program, but when the compiler sees the name FruitClass in these lines:
FruitClass RedFruit();
FruitClass GreenFruit();
the compiler has no idea what FruitClass is unless you include FruitClass.h before those lines are included, so that could be another problem.
3) If you put those lines in every file, it's equivalent to doing this:
Code:
int main()
{
int num;
int num;
int num;
return 0;
}
and the compiler won't let you do that and it will give you redfinition errors.
4)Lastly, don't use global variables--problem solved. If a function needs a variable, then list it as a parameter, and send the variable to the function when you call the function. Using global variables means your functions will have external dependencies, which is generally bad program design. You want your functions to be stand alone black boxes, where you feed them some data, they do something, and then they return some data. In that way, your functions can be reused in any program.
Here is a simple one file program:
Code:
#include <iostream>
using namespace std;
class Apple
{
private:
int size;
public:
Apple()
{
size = 1;
}
void show()
{
cout<<size<<endl;
}
};
Apple a; //global variable
int main()
{
a.show(); //1
return 0;
}
Dividing the program into multiple files:
Code:
//Apple.h
class Apple
{
private:
int size;
public:
Apple();
void show();
};
Code:
//Apple.cpp
#include <iostream>
#include "Apple.h"
using namespace std;
Apple::Apple()
{
size = 1;
}
void Apple::show()
{
cout<<size<<endl;
}
Code:
//main.cpp
#include "Apple.h" //makes the name Apple known to the compiler
Apple a;
int main()
{
a.show(); //1
return 0;
}
Accessing the global variable Apple a from another file:
Code:
//Apple.h
class Apple
{
private:
int size;
public:
Apple(int s);
Apple();
int get_size();
void show();
};
Code:
//Apple.cpp
#include <iostream>
#include "Apple.h"
using namespace std;
extern Apple a; //an instruction to look outside this
//file for the definition of 'a'
Apple::Apple(int s)
{
size = s;
}
Apple::Apple()
{
size = 1;
}
int Apple::get_size()
{
return size;
}
void Apple::show()
{
cout<< a.get_size() + size<<endl; /****/
}
Code:
//main.cpp
#include <iostream>
#include "Apple.h" //makes the name Apple known to the compiler
using namespace std;
Apple a; //global variable
int main()
{
a.show(); //1
Apple b(10);
cout<<endl;
b.show(); //11
return 0;
}
To put Apple a in its own header file, just replace the Apple a declaration with an #include statement.
Code:
//main.cpp
#include <iostream>
#include "Apple.h" //makes the name Apple known to the compiler
#include "globals.h"
using namespace std;
int main()
{
a.show(); //1
Apple b(10);
cout<<endl;
b.show(); //11
return 0;
}
Code:
//globals.h
Apple a;
etc.