startEnc function wont accept any object as a parameter - c++

This is the error I'm getting:
Encounter.h(5,26): error C2061: syntax error: identifier 'TestEnemy'
Encounter.cpp(10,1): error C2511: 'void Encounter::startEnc(TestEnemy)': overloaded member function not found in 'Encounter'
Hero and TestEnemy are classes I've defined in other h and cpp files
Encounter.h:
#pragma once
class Encounter
{
public:
void startEnc(Hero player, TestEnemy e1);
};
Encounter.cpp:
void Encounter::startEnc(Hero player, TestEnemy e1)
{
...
}
I suppose the error could be from another part of my code but visual studio seems to suggest that it's something to do with these. I'll share more code if need be; I just didn't want to inflate this post.

Related

PreTranslateMessage' : function-style initializer appears to be a function definition

In a CDialog class I needed to override
Using VS2008 I've done this several times before using the IDE properties/override and it adds adding this code:
virtual BOOL PreTranslateMessage(MSG* pMsg); // in the header file
BOOL CMyTestDlg::PreTranslateMessage(MSG* pMsg) // in the cpp file
{
return CDialog::PreTranslateMessage(pMsg);
}
The compiler complains with these messages:
error C2059: syntax error : 'constant'
error C2065: 'pMsg' : undeclared identifier
error C2448: 'CMy_TestDlg::PreTranslateMessage' : function-style initializer appears to be a function definition
If I removed the PreTranslateMessage stuff the project compiles and runs as expected.
As the IDE added the override code I'm confused as to what I did wrong, so
I made another simpler Project and did the override in it, and it compiles OK.

c++ interfaces error; identifier "class" is undefined

Using: VS2010 : New Project -> Windows32 Console App -> Empty Project
Language: C++
Problem: I'm attempting a simple interface test with two classes that implement the interface through the 1 virtual method. I also attempt to use constructors within those two classes to set default values. The main problem occurs when attempting to create an object of either class; the IDE complains the identifier "class" is undefined.
I have checked tens of other posts with the same error but most of them are errors in instantiation(proper term?) where they use
Class obj();
instead of
Class obj;
another issue in the other threads was that the header files were not included in main.cpp AND the concrete class(proper term?). Still other issues involved the ordering of includes(base class should be first), or even improper use of constructors/deconstructors.
I have checked over those issues to see if they would work for me but have been unable to figure out what the problem is.
Any insight would be appreciated!
Error: (all in main.cpp)
Error 1 error C2065: 'Human' : undeclared identifier 6 1 interfaceGameTest
Error 2 error C2146: syntax error : missing ';' before identifier 'hum1' 6 1 interfaceGameTest
Error 3 error C2065: 'hum1' : undeclared identifier 6 1 interfaceGameTest
Error 4 error C2065: 'Orc' : undeclared identifier 7 1 interfaceGameTest
Error 5 error C2146: syntax error : missing ';' before identifier 'orc1' 7 1 interfaceGameTest
Error 6 error C2065: 'orc1' : undeclared identifier 7 1 interfaceGameTest
Error 7 error C2065: 'hum1' : undeclared identifier 10 1 interfaceGameTest
Error 8 error C2227: left of '->getHP' must point to class/struct/union/generic type 10 1 interfaceGameTest
Code:
IPc.h
#ifndef IPC_H_HAS_BEEN_INCLUDED
#define IPC_H_HAS_BEEN_INCLUDED
class IPc {
private:
int hp;
int mana;
int endurance;
public:
void setHP(int h) { hp = h; }
void setMP(int m) { mana = m; }
void setEnd(int e) { endurance = e; }
int getHP() { return hp; }
int getMP() { return mana; }
int getEnd() { return endurance; }
virtual int Attack() = 0;
};
#endif
IPc.cpp
#include "IPc.h"
class Human: public IPc
{
public:
Human::Human()
{
this->setHP(10);
this->setMP(5);
this->setEnd(10);
}
Human::~Human(){}
int IPc::Attack() // I have tried just "int Attack()" as well
{
return 1;
}
};
class Orc: public IPc
{
public:
Orc::Orc()
{
this->setHP(20);
this->setMP(0);
this->setEnd(20);
}
Orc::~Orc() {}
int IPc::Attack()
{
return 5;
}
};
main.cpp
#include <iostream>
#include "IPc.h"
int main()
{
Human hum1; // error Human undefined
Orc orc1; // error Orc undefined
int humHP = 0;
humHP = hum1->getHP();
std::cout << "Human HP is: " << humHP << std::endl;
return 0;
}
Edit
I placed
class Human: public IPc { virtual int Attack() };
inside IPc.h (after the base class) and before #endif(also did one for Orc). I also changed
humHP = hum1->getHP();
to
humHP = hum1.getHP();
Which seems to have cleared up the previous issues(Thanks Mat and Rahul). I am now getting a 'class' type redefinition error. I believe this has to do with the include guards. Do I need to surround each individual class with its own set of guards in the .h file or have I implemented them improperly perhaps?
Edit2
I placed both class declarations in their own header files with their own guards. This seems to have resolved the issue of " 'class' type redefinition". Though there is now an error of unresolved external symbol on the virtual attack calls for both derived classes.
You are not allowed to use qualified names in class member declarations. I.e. when declaring the constructor of class Human inside class Human, you are not allowed to call it Human::Human(). It should be just Human(). The same applies to all other methods.
Trying to declare method int IPc::Attack() inside other classes makes no sense at all. It should be just int Attack().
You declared classes Human and Orc inside IPc.cpp. These classes are only visible inside IPc.cpp and nowhere else. In C++ language class declarations for program-wide classes are typically placed in header files, just how you did it with IPc class.

Assigning function pointer within a class gives value type error in C++

I've got this code within Entity.h, which is inherited by other classes. The idea is that in the inheriting classes can change the function either to avoid inheritance or to modify behavior.
public:
Entity(void)
{
updateFunction = emptyFunction;
};
~Entity(void);
protected:
void emptyFunction(){}
void (*updateFunction)();
The problem is that I get this error on compilation:
Error 2 error C2440: '=' : cannot convert from 'void (__cdecl Entity::* )(void)' to 'void (__cdecl *)(void)' c:\documents and settings\administrator\my documents\visual studio 2010\projects\projectname\[projectname]\Entity.h 16 1 [projectname]
I've looked at tutorials all over again, it's been a while since I learned these stuff, but I cannot see some dissimilarity, what could be going wrong? IntelliSense unrelines the '=' symbol as the point of the mistake. I hope I didn't overlook anything.
Thanks a lot in advance!!
You need to decalre pointer function as:
class Entity{
public:
Entity(void)
{
updateFunction = &Entity::emptyFunction;
// ...
(this->*updateFunction)(); //Call
};
~Entity(void);
protected:
void emptyFunction(){}
void (Entity::*updateFunction)();
};
In fact updateFunction is not a normal function and it's a class member function.

Pass sprite to function cocos2dx

I am making pong in cocos2dx and would like to make a function that would take a sprite as an input together with the direction of movement. VC2012 spits out an error on compilation
Error 1 error C2061: syntax error : identifier 'Sprite' (..\Classes\AppDelegate.cpp)
Error 2 error C2061: syntax error : identifier 'Sprite' (..\Classes\HelloWorldScene.cpp)
Error 4 error C2660: 'HelloWorld::movePaddle' : function does not take 2 arguments
Error 5 error C2511: 'void HelloWorld::movePaddle(cocos2d::Sprite *,int)' : overloaded member function not found in 'HelloWorld'
Here is the function:
void HelloWorld::movePaddle(Sprite *paddle, int direction) {
paddle->runAction(MoveBy::create(1,Point(0,15*direction)));
}
Here is the function prototype in the .h file:
void HelloWorld::movePaddle(Sprite *paddle, int direction);
What am I doing wrong?
I apologise if this question seems stupid, I'm very new.

Converting from var to var&

How can I convert a variable of type var or var* to var&
I've to use a function which takes an object of var class(suppose there's a class). The example code was given like this:-
testFunc(false, namespace1::namespace2::var(), 100);
in function declaration it says that the second parameter is of type namespace1::namespace2::var&, I can create namespace1::namespace2::var or namespace1::namespace2::var*, but how do I create namespace1::namespace2::var&?
I know that's too basic question but I couldn't figure it out.
Edit:
I've tried using just var, but it gives some odd errors. I am pretty sure that's some kind of fault in the function I am using. Here're the errors:-
Error 3 error C2825: 'CType': must be a class or namespace when followed by '::'
Error 4 error C2039: 'TypeCode' : is not a member of '`global namespace''
Error 5 error C2146: syntax error : missing ',' before identifier 'TypeCode'
Error 6 error C2065: 'TypeCode' : undeclared identifier
Error 7 error C3203: 'CustomType' : unspecialized class template can't be used as a template argument for template parameter 'Base', expected a real type
Edit 2
I thought it'd be hard to answer if I include real code, since it's complicated. But have a look if it helps. The real function's signature is like this:-
virtual bool opRaiseEvent(bool reliable, const Common::Hashtable& parameters, nByte eventCode, nByte channelID=0, int* targetPlayers=NULL, short numTargetPlayers=NULL);
and the example code used the function like this:-
mLoadBalancingClient.opRaiseEvent(false, ExitGames::Common::Hashtable(), 100);
which was working fine. But now I want to add data to the HashTable, so I need to create an object of it and then pass it to the function. It's not accepting a pointer or normal variable. I don't know why it's working with just HashTable().
It means the second parameter is passed by reference. You have to simply pass:
namespace1::namespace2::var
This should work :
const Common::Hashtable param = namespace1::namespace2::var();
opRaiseEvent(false, param, 100);
namespace1::namespace2::var v;
testFunc(false, v, 100);