C++

Started by
26 comments, last by Shannon Barber 19 years, 6 months ago
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();
		 

};


;)
Advertisement
class Core{    CSnake snake;//declare snake is a member herepublic:    Core() : snake(30, 30) {//snake is constructed here, in the constructor (c:    }    int idleEvents();};
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

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

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
;)
What does you snake class look like? (snake.h) You probably don't have a constructor.
#include "CSnake\Snake.h"
Shouldn't this be: #include "CSnake\\Snake.h"?
____________________________________________________________Programmers Resource Central
It's a linker error, so his constructors probably don't have any bodies.
Are you linking against Snake.cpp ? <- just a guess
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...
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 3class 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 
;)

This topic is closed to new replies.

Advertisement