Results 1 to 7 of 7
My source files aren't compiling correctly. It's been a while since I've done this, so I might be making some dumb mistake. Thank you for any help you can offer. ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 08-29-2006 #1Just Joined!
- Join Date
- Aug 2006
- Posts
- 4
C++ Compile with Eclipse
My source files aren't compiling correctly. It's been a while since I've done this, so I might be making some dumb mistake. Thank you for any help you can offer. I'm using an Eclipse C++ Managed Make project.
In Main.cpp, I #include "Initialize.h"
In Initialize.h, I #include <string>, <vector>, <iostream>, and "Data.h"
In Data.h, I make several classes, function prototypes, and a couple global variables
With just that the program compiles fine. It doesn't do anything yet but no warning or errors. I tested a simple hello world from Main and it runs just fine.
Then I added Data.cpp to my project where I #include "Data.h" and define all of functions that are in Data.h. Now the project does not compile and there are many errors in both Data.h and Data.cpp. Every string, vector, cin, cout, and anything defined in Initialize.h is undefined. (These errors do not appear in Main.cpp) If I edit Data.h to #include "Initialize.h", or <string>, or <vector>, or <iostream> I get a bunch of redefinition errors.
In Initialize.h, I'm using a #ifndef INITIALIZE_H_ #define INITIALIZE_H_ so there should be no reason for anything to be redifined. It seems as though, when I added another cpp file, it is compiling two seperate programs, one beginning from Main.cpp and the other from Data.cpp, and then trying to squish them together. How do I resolve the dependencies for Data.h and Data.cpp without redifining them? Why can't the program compile from Main.cpp and include the other files (only once) in the correct order?
- 08-29-2006 #2
It's admittedly been a long time since I've written C++ code, but I'm confused as to why you find it necessary to create a separate header file for all your includes. Wouldn't explicitly defining all the necessary includes in each of your CPP files be a simpler way to get everything working? More tedious perhaps, but less complicated I would think.
Registered Linux user #270181
TechieMoe's Tech Rants
- 08-29-2006 #3Just Joined!
- Join Date
- Aug 2006
- Posts
- 4
Well as I said, when I add #include <string> to Data.h I get multiple definition errors. I wouldn't mind too much if I put the #includes in every file but so far that's not working.
This project was a test for a much bigger project which may end up with fifty or more source files. It would be pretty nice not to have a million includes at the top of every file, but for now I just want something to run.
- 08-29-2006 #4Ah, well that was going to be my next question. So even if you explicitly include all the headers you still get errors?
Originally Posted by KilltheSand Registered Linux user #270181
TechieMoe's Tech Rants
- 08-30-2006 #5Linux User
- Join Date
- Oct 2004
- Location
- /dev/random
- Posts
- 404
If you don't mind, can you please share the code and the exact error messages from the compilation?
That would help us find the exact problem.The Unforgiven
Registered Linux User #358564
- 09-02-2006 #6Just Joined!
- Join Date
- Aug 2006
- Posts
- 4
// Main.cpp
// Includes
#include "Initialize.h"
// Functions
int main () {
int x;
cout << "Hello" << endl;
cin >> x;
cout << x * x << endl;
return 0;
}
---
// Initialize.h
#ifndef INITIALIZE_H_
#define INITIALIZE_H_
// Includes
#include <string>
#include <vector>
#include <iostream>
using namespace std;
#include "Data.h"
// Function Prototypes
bool init();
void deinit();
void killall();
#endif /*INITIALIZE_H_*/
---
// Data.h
#ifndef DATA_H_
#define DATA_H_
// Structures
struct Size {
int w;
int h;
};
struct EdgeAttribute {
int l;
int r;
int u;
int d;
};
struct MotionAttribute {
double h;
double v;
double a;
};
struct PhysicsAttribute {
double mass;
MotionAttribute speed;
MotionAttribute maxspeed;
MotionAttribute acceleration;
};
// Classes
class State {
public:
// Constructors
State();
// Deconstructor
~State();
// Public Variables
string name;
string description;
string ai;
// Public Function Prototypes
void actions();
};
class Object {
public:
// Constructors
Object();
// Deconstructor
~Object();
// Public Variables
State state;
string name;
Size size;
EdgeAttribute position;
bool active;
PhysicsAttribute physics;
// Public Function Prototypes
void actions();
};
#endif /*DATA_H_*/
---
// Data.cpp
#include "Data.h"
// Class Definitions
// State Class
// Constructors
State::State() {
// Default Constructor
}
// TODO: Add required constructors
// Deconstructor
State::~State() {
// TODO: Define function
}
// Public Functions
void State::actions() {
// TODO: Define function
}
// Object Class
// Constructors
Object::Object() {
// Default Constructor
}
// TODO: Add required constructors
// Deconstructor
Object::~Object() {
// TODO: Define function
}
// Public Functions
void Object::actions() {
// TODO: Define function
}
// Private Functions
void Object::die() {
// TODO: Define function
}
- 09-02-2006 #7Just Joined!
- Join Date
- Aug 2006
- Posts
- 4
The code as above won't compile. Every string defined in Data.h says
error" 'string' does not name a type
if I add this to either the top of Data.h or Data.cpp
#include <string>
#include <vector>
#include <iostream>
using namespace std;
then in Data.cpp I have an error at void State::actions() {
first defined here
I receive an error if I add any global veriables to Data.h such as int global;
multiple definition of 'global'


Reply With Quote
