Jump to content
  • Advertisement
Sign in to follow this  
spree

C++

This topic is 5142 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I did a fully working snake class ( which was working on a windows console app ) My problem is, that when i declair CSnake snake(30,30) in my new game Core class there is an error : error C2059: syntax error : 'constant' Can't I creat instancess in within the class's defenition ??

#ifndef _CORE_H_
#define _CORE_H_


#include "CSnake\Snake.h"

class Core
{
	
CSnake snake(30,30); //  THE ERROR IS HERE

	public:

		 Core();


		 int idleEvents();
		 

};


Share this post


Link to post
Share on other sites
Advertisement
class Core
{
CSnake snake;//declare snake is a member here
public:
Core() : snake(30, 30) {//snake is constructed here, in the constructor (c:
}
int idleEvents();
};

Share this post


Link to post
Share on other sites
Try this:

#ifndef _CORE_H_
#define _CORE_H_

#include "CSnake\Snake.h"

class Core
{
CSnake snake;
public:
Core() : snake(30,30) {}

int idleEvents();
};




I think you are trying to initialize the CSnake instance too early, my modification just moves that into the constructor, so snake will be initialized as soon as you create the Core.

[edit]I see petewood beat me to it.[/edit]

Hope this helps,

SwiftCoder

Share this post


Link to post
Share on other sites
I have changed the code to this, but i get this error:

"WinSnake error LNK2019: unresolved external symbol "public: __thiscall CSnake::CSnake(int,int)" (??0CSnake@@QAE@HH@Z) referenced in function "public: __thiscall Core::Core(void)" (??0Core@@QAE@XZ)
"

Like I said, the snake class is fully working. What am I doing wrong ???


#ifndef _CORE_H_
#define _CORE_H_


#include "CSnake\Snake.h"

class Core
{

CSnake snake;

public:

Core():snake(50,50)
{
};


int idleEvents();


};


#endif








Even if do just this :


#ifndef _CORE_H_
#define _CORE_H_


#include "CSnake\Snake.h"

class Core
{

CSnake snake;

public:

Core()
{
};


int idleEvents();


};


#endif



The compiler gives me 2 erros

WinSnake error LNK2019: unresolved external symbol "public: __thiscall CSnake::CSnake(void)" (??0CSnake@@QAE@XZ) referenced in function "public: __thiscall Core::Core(void)" (??0Core@@QAE@XZ)

and

WinSnake fatal error LNK1120: 1 unresolved externals

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
It's a linker error, so his constructors probably don't have any bodies.

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
It's a linker error, so his constructors probably don't have any bodies.

Good point. I missed that...

Share this post


Link to post
Share on other sites
Here are the relevant files:


core.h

#ifndef _CORE_H_
#define _CORE_H_


#include "CSnake\\Snake.h"

class Core
{

CSnake snake;

public:

Core()
{
};


int idleEvents();


};


#endif





core.cpp

#include "Core.h"




/*
idleEvents : this is where the idle events of the game would accure.
Events and Game states which happen when there is no input.

*/


int Core::idleEvents()
{


return 1;
}



/*
Core::Core():snake(50,50) // the construcots body is in the core.h now
{


}*/






snake.h


#ifndef SNAKE_H
#define SNAKE_H

#include "MCPoint.h"
#include "CNode.h"
#include "CList.h"

#define FREE_BLOCK 0
#define BORDER_BLOCK 1
#define FOOD_BLOCK 2
#define SNAKE_BLOCK 3

#define NORTH 0
#define EAST 1
#define SOUTH 2
#define WEST 3




class CSnake
{
public:
int L,H;// Length , Heigth



int direction;// the direction of the snake


int map[100][100];//map matrix


CList snake;// the snake is a linked list



CSnake(int,int);
CSnake();


void setMapDimantion(int len,int hig);

void initMap();

void setSnake(MCPoint);




bool updateSnake();// update the snakes movement by the member variable: direction


int checkNextBlock();
int checkThisBlock(MCPoint);

// generate food block

MCPoint getRandomFreeBlock();//looks for a FREE_BLOCK at a random spot at the map
void addFoodBlock();//adds a food block to the map



private:
// Linked List related functions
void flip();// replaces the head with the tail

void progressSnake();



};


#endif


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!