This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2012-08-08 13:14:58

16Skittles
Scratcher
Registered: 2009-08-26
Posts: 1000+

C++ Undefined Reference in a Constructor?

Well I got very little help from TIMaC, and I was told to try here. I am working on a game in C++, and need help on an error. I had before used a non-standard method of initializing my classes by using a function like

Code:

class foo{
public:
void initialize(int getx, int gety, int getz);
int x;
int y;
int z;
};
void foo::initialize(int getx, int gety, int getz){
x=getx;
y=gety
z=getz;
}

Recently I have been getting errors with my x and y integers randomly changing. One thing that I found was that these things may not have been initialized properly, or may have had a buffer overflow. To avoid the non-initialized problem I have been changing to the preferred method of using constructors. (Also because I had earlier problems with Segfaults, and this is how I fixed them)
I am trying to make a constructor to my class Sector.

Code:

class Tile{public: int x; int y; void update(int, int); bool initialized; int type; Tile(int, int, char);};


class sector{
    
    public:
        void initialize(int getx, int gety);
        void update();
        void create(int biome);
        void save();
        void load();
        sector(int, int);
    private:
        bool initialized;
        void getFileName();
        char* filename;
        int x;
        int y;
        Tile tilesArray[10][10];
    
};

sector::sector(int getx, int gety){
    filename = new char;
    x=getx;
    y=gety;
}

When I compile this code, it gives me this error:

In constructor 'sector::sector(int, int)':
no matching function to call to 'Tile::Tile()'
candidates are: Tile::Tile(const Tile&)
          Tile::Tile(int, int, char)
[Build Error] [sector.o] Error1

I can't really find any help online by searching, maybe someone here can help me.  hmm


http://16skittles.tk/sig.png
Are you a student? Check out OnSchedule!

Offline

 

#2 2012-08-08 13:38:31

beetle16
Scratcher
Registered: 2010-03-28
Posts: 9

Re: C++ Undefined Reference in a Constructor?

Since you've got an Array of type Tile in your sector class, these must be initialized. This is normally done by the standard constructor of class Tile.

As you defined a custom constructor for Tile (int, int, char), the compiler does not generate the standard constructor for you. Therefore the compiler cannot find it.

So you have to insert that for class Tile. The standard constructor has no parameters.

Code:

class Tile
{
 public:
   ...
  Tile() { ... }
};

greetings,
  beetle16

Offline

 

#3 2012-08-08 14:08:57

16Skittles
Scratcher
Registered: 2009-08-26
Posts: 1000+

Re: C++ Undefined Reference in a Constructor?

beetle16 wrote:

Since you've got an Array of type Tile in your sector class, these must be initialized. This is normally done by the standard constructor of class Tile.

As you defined a custom constructor for Tile (int, int, char), the compiler does not generate the standard constructor for you. Therefore the compiler cannot find it.

So you have to insert that for class Tile. The standard constructor has no parameters.

Code:

class Tile
{
 public:
   ...
  Tile() { ... }
};

greetings,
  beetle16

This solved that error, thanks!  smile
Now I need to go and modify more stuff to fix my other problems.  tongue


http://16skittles.tk/sig.png
Are you a student? Check out OnSchedule!

Offline

 

Board footer