-3

Can I do this? it tells me unresolved external symbol WTH!

I really need to initialise the value of GameObjectCount to Zero.

Also, when I use the Default constructor to build an object, I cant access any of its members!

WHY?

main.cpp

#pragma once

#include <iostream>
#include <string>

#include "MapTile.h" 
#include "GameOb.h"

MapTile backGRIND("Name");
GameOb test();

int main(int, char**){   ////this main is for SDL2////

    std::cout << backGRIND.texturePath << std::endl;
    std::cout << backGRIND.GameObjectCount << backGRIND.x << backGRIND.ObjectID << std::endl;

GameOb.cpp

#include "GameOb.h"


int GameOb::GameObjectCount = 0;

GameOb::GameOb()
{
    GameOb::GameObjectCount++; 

    GameOb::x = 0;
    GameOb::y = 0;
    GameOb::ObjectID = GameObjectCount; 

    GameOb::texturePath = std::string("Ressources/GameObjects/"); 

    std::cout << "Object has been created with Object ID : " << GameOb::ObjectID << std::endl;
}

GameOb::GameOb(int x, int y)
{
    GameOb::GameObjectCount++;

    GameOb::x = x;
    GameOb::y = y;
    GameOb::ObjectID = GameObjectCount;     

    GameOb::texturePath = std::string("Ressources/GameObjects/");

    std::cout << "Object has been created with Object ID : " << GameOb::ObjectID << std::endl;
}

GameOb.h

#pragma once

#include <string>
#include <iostream>  

class GameOb
{
public:

    GameOb();
    GameOb(int, int);

    ~GameOb();

    int x;
    int y;
    int ObjectID;

    std::string texturePath;

    void update(int, int, int);

    static int GameObjectCount;
};

MapTile.cpp

#include "MapTile.h"



MapTile::MapTile(std::string name)
{       
    GameOb::texturePath += std::string("MapTile/");
}


MapTile::~MapTile()
{
}

MapTile.h

#pragma once

#include <string>
#include <iostream>

#include "GameOb.h"       

class MapTile :
    public GameOb     
{
public:
    MapTile(std::string);



    ~MapTile();    

};

OK SO ITS DONE

why cant I access the test instances members?? when i write test.(nothing shows)?? help ... im really new at this.

pnuts
  • 58,317
  • 11
  • 87
  • 139
HgMerk
  • 71
  • 11
  • 1
    [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask) – R Sahu Mar 11 '15 at 05:22
  • 2
    Err, what's in "GameOb.h"? – user207421 Mar 11 '15 at 05:35
  • The answer to this question http://stackoverflow.com/questions/1926605/how-to-count-the-number-of-objects-created-in-c might solve your problem of getting a count of how many objects have been created. There are a few possible solutions. But EJP has probably answered your question. – Arunas Mar 11 '15 at 06:58
  • Sorry for not posting .h :S was late :( – HgMerk Mar 11 '15 at 18:35
  • I am completely making things overcomplicated for nothing!!! GameOb needs no x.y(just need 1 constructor not two)!!! Sorry for wasting ur guys time ... – HgMerk Mar 11 '15 at 18:55
  • I've been doing this for about a month now, if anyone wants to look over this code for fun, tell me what I do wrong, if I have a bad style or anything.. Thank you in adv – HgMerk Mar 11 '15 at 18:59
  • Whew! 3 years such improvement :D – HgMerk Jun 21 '18 at 11:34

1 Answers1

1

Unless GameOb.h contains at a minimum the following:

class GameOb
{
    static int GameCount;
    static int ObjectID;
    static int x;
    static int y;
    static std::string texturePath;

    GameOb();

    GameOb(int x, int y);
}

your code cannot possibly compile.

I don't consider it likely or desirable that x and y and ObjectID and texturePath are really meant to be static, but that's the implication of the code you posted. So you also need to look up the correct way to initialize data members in a constructor, and the correct way to refer to non-static members.

user207421
  • 305,947
  • 44
  • 307
  • 483