-4

I am trying to send a object to another class although i am having problems.

Error: In Constructor StartGame::StartGame(EntitySystem&):

error: class StartGame does not have any field named ES

Main.cpp

#include <iostream>
#include "EntitySystem.h"
#include "StartGame.h"

using namespace std;

int main()
{
    EntitySystem ES;
    StartGame SG(ES);
    SG.Start();
    return 0;
}

StartGame.h

#ifndef STARTGAME_H
#define STARTGAME_H
#include "EntitySystem.h"
#include <iostream>

using namespace std;
class StartGame
{
public:
    StartGame(EntitySystem&);
    void ClassSelection();
    void Start();
    virtual ~StartGame();
};

StartGame.cpp

#include "StartGame.h"
#include "EntitySystem.h"
#include "windows.h"
#include "stdlib.h" // srand, rand
#include <iostream>
#include <ctime> //time

using namespace std;

StartGame::StartGame(EntitySystem& ES) : ES(ES)
{
}

I did some googling and couldnt figure it.
Thanks in advance.

Community
  • 1
  • 1
  • 6
    Your problem is class `StartGame` has no member variable named `ES`, as the error message says. – David Brown Aug 11 '13 at 17:35
  • 3
    I suggest you read a good C++ book. [Start here](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). You are confusing member variables with non-member variables, and apparently you don't know what scoping is, which is a fundamental deficiency of your C++ knowledge. –  Aug 11 '13 at 17:38

2 Answers2

2

Because in your the member-initialization-list of your constructor you are trying to initialize a member named ES, which does not exist...

You can fix it by doing this in the declaration of your class :

class StartGame
{
    public:
        StartGame(EntitySystem&);
        void ClassSelection();
        void Start();
        virtual ~StartGame();
    protected:
    private:
        EntitySystem ES;
    //  ^^^^^^^^^^^^^^^^
};

// in the .cpp
StartGame::StartGame(EntitySystem& ES)
    : ES(ES)
//    ^^ Now this member exists
{
}

But I suggest you to rename either your parameter or your member because it may be confusing to have the same name for both...

Pierre Fourgeaud
  • 14,290
  • 1
  • 38
  • 62
0

Your code does this:

StartGame::StartGame(EntitySystem& ES)
    : ES(ES)

Let's break this down:

":"       <-  means "Memberwise initialization"
"ES("     <-  means "call the constructor for member 'ES'"
"(ES)"    <-  these are the arguments to pass to the constructor
"{ ... }" <-  here is the constructor body itself.

The trouble is, your class does not have a member called "ES".

But you also have a secondary problem.

"ES(ES)"

If you had a member variable called "ES", there would be a conflict here.

There are several widely used practices that you might want to employ to avoid problems like this in future.

  1. Use upper-camelcase ("SomeClass") for class, struct, type definitions.
  2. Use prefixes for non-local variables: "m_" for members, "s_" for statics, "g_" for globals, (some people also use "u_" for unions, "um_" for union members, etc).
  3. Use a '_' suffix for function arguments, e.g. void myFunction(int arg1_, int arg2_) { ...
  4. Use upper-case for functions and choose a convention for naming convention for "getter" and "setter" functions.

e.g.

static size_t s_counter = 0; // count of how many things we've done.
extern size_t g_nowServing;  // imported from another module.

class Customer {
    size_t m_ticketNo;       // which ticket number I pulled
    std::string m_licensePlate;
public:
    Customer(size_t ticketNo_, const char* licensePlate_)
        : m_ticketNo(ticketNo_)
        , m_licensePlate("")
    {
        // somewhat artificial implementation for demonstrative purposes.
        char licensePlate[8] = "\0\0\0\0\0\0\0";
        for (size_t i = 0; i < sizeof(licensePlate); ++i) {
            if (!isalnum(licensePlate_[i]))
                throw CustomerException("invalid character in license plate");
            licensePlate[i] = licensePlate_[i];
        }
        if (licensePlate[sizeof(licensePlate) - 1] != '\0')
            throw CustomerException("invalid license plate -- too long");

        m_licensePlate = licensePlate;
    }
};

This makes it much easier to immediately diagnose the problem in your code:

StartGame::StartGame(EntitySystem& es_)
    : m_es(es_)
{
}

This would have produced the compiler error error: class StartGame does not have any field named m_es and bingo - you would have been able to home in on the problem immediately.

kfsone
  • 23,617
  • 2
  • 42
  • 74