Need help with "primary expression" error

This is a discussion on Need help with "primary expression" error within the C++ Programming forums, part of the General Programming Boards category; Hi everyone. I pretty new to C/C++. Heres my code: Code: #include "Environment.h" int Environment :: nExist = 0; int ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    6

    Need help with "primary expression" error

    Hi everyone. I pretty new to C/C++. Heres my code:

    Code:
    #include "Environment.h"
    
    int Environment :: nExist = 0;
    int Environment :: nCreated = 0;
    
    Environment :: Environment(void){
            serNo = ++nCreated;
            nExist++;
    
            char buffer[100];
            sprintf(buffer, "Environment%d", serNo);
            label = new char[strlen(buffer) + 1];
            strcpy(label, buffer);
            return;
    }//Environment :: Environment(void)
    Code:
    #ifndef SIMULATION_H
    #define SIMULATION_H
    
    #include "Environment.cpp"
    
    using namespace std;
    
    class Simulation {
            private:
            Environment env;
            int serNo;
            static int nExist, nCreated;
    
            public:
            Simulation(void);
            Simulation(const Environment *);
    Simulation(const Simulation&);
            ~Simulation();
    
            const Simulation & operator = (const Simulation&);
    
            void print(void) const;
    };
    Code:
    #include "Simulation.h"
    
    int Simulation :: nExist = 0;
    int Simulation :: nCreated = 0;
    
    Simulation :: Simulation(void){
            serNo = ++nCreated;
            nExist++;
            env = new Environment(void);
    
            return;
    }//Simulation :: Simulation(void)
    Heres the Error:
    Code:
    Simulation.cpp: In constructor `Simulation::Simulation()':
    Simulation.cpp:9: error: expected primary-expression before "void"
    Any help would be greatly appreciated

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    Never ever include a .cpp file.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    6
    O.K. I changed it to #include "Environment.h".
    Still getting the same error though.
    Any other thoughts?

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    env = new Environment(void);
    don't use void when calling a function.

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    6
    Thanks. I took out the void
    Code:
    #include "Simulation.h"
    
    int Simulation :: nExist = 0;
    int Simulation :: nCreated = 0;
    
    Simulation :: Simulation(void){
            serNo = ++nCreated;
            nExist++;
            env = new Environment();
    
            return;
    }//Simulation :: Simulation(void)
    and now I've got this error.

    Code:
    Simulation.cpp: In constructor `Simulation::Simulation()':
    Simulation.cpp:9: error: no match for 'operator=' in '((Simulation*)this)->Simulation::env = (((Environment*)operator new(8u)), (<anonymous>->Environment::Environment(), <anonymous>))'
    Environment.h:22: note: candidates are: const Environment& Environment::operator=(const Environment&)

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    new returns a pointer.
    Code:
    Environment *env;
    make sure to delete it in your destructor.

  7. #7
    Registered User
    Join Date
    Sep 2007
    Posts
    6
    That makes sense. I still have one more error:
    Code:
    Simulation.cpp: In constructor `Simulation::Simulation()':
    Simulation.cpp:9: error: no match for 'operator*' in '*((Simulation*)this)->Simulation::env'
    Code:
    #ifndef SIMULATION_H
    #define SIMULATION_H
    
    #include "Environment.h"
    
    using namespace std;
    
    class Simulation {
            private:
            Environment *env;
            int serNo;
            static int nExist, nCreated;
    
            public:
            Simulation(void);
            Simulation(const Environment *);
    Simulation(const Simulation&);
            ~Simulation();
    
            const Simulation & operator = (const Simulation&);
    
            void print(void) const;
    };
    Code:
    #include "Simulation.h"
    
    int Simulation :: nExist = 0;
    int Simulation :: nCreated = 0;
    
    Simulation :: Simulation(void){
            serNo = ++nCreated;
            nExist++;
            *env = new Environment();
    
            return;
    }//Simulation :: Simulation(void)

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    env = new Environment();
    http://www.cprogramming.com/tutorial/lesson6.html

    Quote Originally Posted by me
    make sure to delete it in your destructor.
    Last edited by robwhit; 09-10-2007 at 11:51 PM.

  9. #9
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    Quote Originally Posted by robwhit View Post
    new returns a pointer.
    Code:
    Environment *env;
    make sure to delete it in your destructor.
    I also want to ask, is there any particular reason the OP feels the Environment should be dynamically allocated? If the lifetime of the environment is always the same as the lifetime of the Simulation, it would probably be better to hold the Environment by value (i.e., don't "new" it).

    This also relieves you of the responsibility of delete'ing it in the Simulation destructor, as well as having to write an assignment operator.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 02:17 PM
  3. How to monitor process creation?
    By markiz in forum Windows Programming
    Replies: 31
    Last Post: 03-17-2008, 03:39 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21