GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 10-Sep-2007, 20:34
kspill2 kspill2 is offline
New Member
 
Join Date: Sep 2007
Posts: 1
kspill2 is on a distinguished road

Need help with "primary-expression" error


Hi everyone. I pretty new to C/C++. Heres my code:
CPP / C++ / C 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)
CPP / C++ / C 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;
};
CPP / C++ / C 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:
CPP / C++ / C Code:
Simulation.cpp: In constructor `Simulation::Simulation()':
Simulation.cpp:9: error: expected primary-expression before "void"

Any help would be greatly appreciated
  #2  
Old 11-Sep-2007, 00:25
Kimmo Kimmo is offline
Member
 
Join Date: Mar 2007
Location: Finland
Posts: 229
Kimmo has a spectacular aura aboutKimmo has a spectacular aura about

Re: Need help with "primary-expression" error


Try
CPP / C++ / C Code:
env = new Environment();
instead of
CPP / C++ / C Code:
env = new Environment(void);
(Assuming that is the line in question.)

I don't know if it helps, but looks like it could.

Other than that... There are just so many things "weird" or "wrong" or "missing" in your code, I wouldn't know where to begin listing.
  #3  
Old 11-Sep-2007, 06:13
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 400
Peter_APIIT is on a distinguished road

Re: Need help with "primary-expression" error


Simulation(const Environment *);

Use reference instead of pointer or pass by value.

I hope this help.
__________________
Linux is the best OS in the world.
  #4  
Old 14-Sep-2007, 10:54
Kimmo Kimmo is offline
Member
 
Join Date: Mar 2007
Location: Finland
Posts: 229
Kimmo has a spectacular aura aboutKimmo has a spectacular aura about

Re: Need help with "primary-expression" error


I'll try to point out some things to consider in the future, when posting question and in general.
CPP / C++ / C Code:
#include "Environment.h"
We have no way of knowing what exactly is contained in "Environment.h", so including this kind of #include directives in code you post here on the forums without actually including the code from "Environment.h" is useless. This also renders the the following code useless because, apparently, the following code depends on the class inside "Environment.h" to which we have no access.

So considering your post here, this piece of code is just useless overhead for us.

CPP / C++ / C Code:
Environment :: Environment(void)
The C++ way of telling "no arguments" is just (), not (void).

CPP / C++ / C Code:
        char buffer[100];
        sprintf(buffer, "Environment%d", serNo);
        label = new char[strlen(buffer) + 1];
        strcpy(label, buffer);
        return;
My C knowledge is nonexistant, but does this mean you put "Environment" + serNo to buffer, then allocate memory for label, and then copy buffer to label?

If label is just a "string", you might want to take a look at the C++ string class. Also, did you take into account the use of dynamic memory allocation in your destructor, copy constructor and assignment operator? That's another reason why using std::string would be handier.

You don't actually need the return statement at the end.

CPP / C++ / C Code:
#ifndef SIMULATION_H
#define SIMULATION_H
You miss the terminating #endif.

CPP / C++ / C Code:
#include "Environment.cpp"
I can't really say anything about this. Usually, I think, we #include only headers and compile and link source files. I don't know how this would affect your whole program. Your Environment.cpp doesn't seem to be safeguarded with #ifndef (and why should it.. We usually just link them) though, so including this should at least create some compile errors right?

In any way, your class Simulation doesn't need to know about the implementation of class Environment. It just needs the declaration.

CPP / C++ / C Code:
using namespace std;
Umm... What is this for? I mean, I don't see any names from namespace std in your class declaration, so why would you need this? It's also generally a bad habit to include the whole namespace, but I guess you haven't been to namespaces yet, so it's quite okay. (Except for the fact you don't use the names.)

CPP / C++ / C Code:
class Simulation {
        private:
        Environment env;
You need a decent Environment assignment operator and/or copy constructor to properly copy this member object.

CPP / C++ / C Code:
        ~Simulation();
This wouldn't compile anyway without the definition of destructor. If you provide the declaration, you have to provide the definition.

CPP / C++ / C Code:
        const Simulation & operator = (const Simulation&);
The return value should probably be without the const qualifier..?


cpp]
#include "Simulation.h"

CPP / C++ / C Code:
        env = new Environment(void);
Dynamic memory allocation and non-defined destructor = bad things. Besides, the way I see it, your env is just an Environment data member, not a pointer to an Environment. So this shouldn't actually compile right?
 

Recent GIDBlogFirst week of IA training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Linked Lists advice request promsan C Programming Language 74 23-May-2007 08:29
Major newbie problem cynack MS Visual C++ / MFC Forum 1 08-Apr-2007 11:25
Winsock error when compiling FLTK 2.0 Projects mauriciorossi FLTK Forum 3 16-Aug-2005 10:18
Help with syntax errors PeteGallo C Programming Language 7 08-Aug-2005 20:30
What is "Ambigious symbol" ??*( a compilation error) small_ticket CPP / C++ Forum 2 07-Jan-2005 21:10

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 14:32.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.