Find the answer to your Linux question:
Results 1 to 7 of 7
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    Question 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?

  2. #2
    Linux Guru techieMoe's Avatar
    Join Date
    Aug 2004
    Location
    Texas
    Posts
    9,496
    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

  3. #3
    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.

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Guru techieMoe's Avatar
    Join Date
    Aug 2004
    Location
    Texas
    Posts
    9,496
    Quote Originally Posted by KilltheSand
    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.
    Ah, well that was going to be my next question. So even if you explicitly include all the headers you still get errors?
    Registered Linux user #270181

  6. #5
    Linux 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

  7. #6
    // 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
    }

  8. #7
    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'

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •