Sign in to follow this  
spree

C++

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
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
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
Guest Anonymous Poster
In the CSnake class change this:

CSnake();

to this:

CSnake(void);

This makes a difference in some compilers.

Share this post


Link to post
Share on other sites
i have added the void, all the files are compiled well, but when i make a build, it gives me 5 linker errors

these errors are from CSnake's "sub" files , like a linked list and all the rest. But it worked, the thing is the snake worked.
I just wanted to imigrate it into win 32 API :/ :\
Shall i upload the whole code?

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

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

WinSnake error LNK2019: unresolved external symbol "public: void __thiscall CList::pushHead(class CNode *)" (?pushHead@CList@@QAEXPAVCNode@@@Z) referenced in function "public: void __thiscall CSnake::setSnake(class MCPoint)" (?setSnake@CSnake@@QAEXVMCPoint@@@Z)

WinSnake error LNK2019: unresolved external symbol "public: __thiscall CNode::CNode(class MCPoint)" (??0CNode@@QAE@VMCPoint@@@Z) referenced in function "public: void __thiscall CSnake::setSnake(class MCPoint)" (?setSnake@CSnake@@QAEXVMCPoint@@@Z)

WinSnake error LNK2019: unresolved external symbol "public: class CNode * __thiscall CList::popBack(void)" (?popBack@CList@@QAEPAVCNode@@XZ) referenced in function "private: void __thiscall CSnake::flip(void)" (?flip@CSnake@@AAEXXZ)

WinSnake fatal error LNK1120: 5 unresolved externals

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
In the CSnake class change this:

CSnake();

to this:

CSnake(void);

This makes a difference in some compilers.

Not in C++ compilers. In C, a function declared to take no parameters (void) is different from one without parameter declarations; in C++ this is not the case.

Share this post


Link to post
Share on other sites
Your microsoft solution file does not include the snake.cpp in its build.

So the file never gets compiled and linked into the executable,
hence the reason why you get the unresolved symbols.

Add snake.cpp to your project files and recompile. It should fix all of your problems.

- Alek

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this