C++ problem with virtual functions - c++

I need to write something in C++. I have problem with virtual functions.
For example, in header file Human.h I have this:
class Human
{
public:
virtual int Age();
Human();
~Human();
}
In Human.cpp file I have this:
#include<iostream>
#include "Human.h"
int Human::Age()
{
return 0;
}
I get these compile errors:
Error 4 error C2371: 'Human::Age' : redefinition; different basic types c:\users\jan\desktop\testc\testc\human.cpp 5 1 TestC
Error 3 error C2556: 'Human Human::Age(void)' : overloaded function differs only by return type from 'int Human::Age(void)' c:\users\jan\desktop\testc\testc\human.cpp 5 1 TestC
Error 2 error C2628: 'Human' followed by 'int' is illegal (did you forget a ';'?) c:\users\jan\desktop\testc\testc\human.cpp 4 1 TestC

You have forgotten to end the class definition with a ;
It should read
class Human
{
public:
virtual int Age();
Human();
~Human();
};
This will likely make the error go away. Also, always read the compiler's output: Error 2 error C2628: 'Human' followed by 'int' is illegal (did you forget a ';'?) c:\users\jan\desktop\testc\testc\human.cpp 4 1 TestC

Related

startEnc function wont accept any object as a parameter

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.

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.

Linked List says ; is missing...but its not

I am trying to make a simple linked list. Everything was going fine, and then all of a sudden, a massacre of errors. I have no clue what I changed to break my code. This is my file that is getting some of the errors:
#pragma once
#include <string>
#include "Node.h"
class LinkedList
{
private:
Node *head;
public:
LinkedList();
~LinkedList();
void AddNode(int);
string GetList(); //missing ';' before identifier 'GetList'
bool Contains(int);
void Remove(int);
};
It claims that I am missing a semi-colon on the line above string GetList();, or so it looks...but obviously I am not. That exact error is:
Error 1 error C2146: syntax error : missing ';' before identifier 'GetList' c:\...\linkedlist.h 15 1 ProjectName
The other error on that line is:
Error 2 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\...\linkedlist.h 15 1 ProjectName
But it is identified as a string return type.
In LinkedList.cpp, this is the GetList() method:
string LinkedList::GetList(){
string list;
Node *currentNode = head;
while (currentNode->next_node){
currentNode = currentNode->next_node;
list += currentNode->get_value() + " ";
}
return list;
}
It all appears good there, but in the method header, I am getting the following 2 errors:
Error 4 error C2556: 'std::string LinkedList::GetList(void)' : overloaded function differs only by return type from 'int LinkedList::GetList(void)' c:\...\linkedlist.cpp 28 1 ProjectName
Error 5 error C2371: 'LinkedList::GetList' : redefinition; different basic types c:...\linkedlist.cpp 28 1 ProjectName
I have gone so far as to create a new project and copy and paste all of my files back in, but that had no effect. I had successfully run GetList() in this program previously.
Does anyone know what in the world is going on here? My IDE is lying to me! (Visual Studio Community 2013 Update 4)
You have using namespace std; somewhere in LinkedList.cpp, but not in LinkedList.h. That's why in the class definition it doesn't know you're referring to std::string when you write string.
I'd recommend to stop using using namespace std; to avoid this kind of problems.
Use std::string, not just string.

Header file compiles multiple times and gets random errors at every compile after the first compile

When I go to build my C++ project, I get 53 errors. However, it's the same list of errors 4 times in a row from one of the 5 header files I have in my project. I checked the output and found that it attempted to compile that one header file 5 times. It appears that the first time was successful. The other 4 times got errors, but they were the same errors over and over again. I followed where the includes of lead to. Based on all of the places that I include that header file, it makes sense that it would try to compile it for every time that it's included.
This is the header file that's getting compiled multiple times. The first successful compile makes sense, but I don't understand why it's getting a bunch of errors every other time it compiles while building the project:
#ifndef TRANSACTION_H
#define TRANSACTION_H
#include <string>
#include "Account.h"
#include "BSTree.h"
using namespace std;
class Transaction
{
public:
Transaction();
Transaction(char type, string firstName, string lastName, int ID, Account* account1, int fund1, Account* account2, int fund2, int amount);
~Transaction();
void setPtrAccounts(BSTree* ptrAccounts);
bool Transact();
private:
static BSTree* ptrAccounts;
char type;
string firstName;
string lastName;
int ID;
Account* account1;
int fund1;
Account* account2;
int fund2;
int amount;
void Deposit();
void History();
void Open();
bool Transfer();
bool Withdraw();
};
#endif
Here's the repeating list of errors. These errors are completely bogus. There's nothing wrong with the code in the above header file:
error C2061: syntax error : identifier 'Account' \thejollybanker\transaction.h 14 1
error C2061: syntax error : identifier 'BSTree' \thejollybanker\transaction.h 16 1
error C2143: syntax error : missing ';' before '*' \thejollybanker\transaction.h 19 1
error C4430: missing type specifier - int assumed. \thejollybanker\transaction.h 19 1
error C2143: syntax error : missing ';' before '*' \thejollybanker\transaction.h 24 1
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int \thejollybanker\transaction.h 24 1
error C2143: syntax error : missing ';' before '*' \thejollybanker\transaction.h 26 1
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int \thejollybanker\transaction.h 26 1
Here's a summary of the output window:
Transaction.cpp
TheJollyBanker.cpp
Transaction.h errors
Fund.cpp
BSTree.cpp
Transaction.h errors
Bank.h
Transaction.h errors
Account.cpp
Transaction.h errors
Generating Code...
How do I get it to only compile it once so that it successfully compiles the first time?
string is part of namespace std. Replace string with std::string everywhere in that header, then it should work.

Initializing object of a class inside another class

I have a class MySeqBuildBlockModule that I am inheriting from: public SeqBuildBlock.
Other than constructor and destructor, this class MySeqBuildBlockModule has a method: prep.
class MySeqBuildBlockModule: public SeqBuildBlock
{
friend class SeqBuildBlockIRns;
public:
MySeqBuildBlockModule (SBBList* pSBBList0, long TI1_In, long TI2_In) // more arguements in this constructor of derived class
: SeqBuildBlock (pSBBList0)
{
TI1 = TI1_In; //in us
TI2 = TI2_In; //in us
pSBBList2 = pSBBList0;
//SeqBuildBlockIRns myIRns_3(pSBBList2); //Defined in libSBB.h
}
//~MySeqBuildBlockModule(){}
virtual bool prep (MrProt* pMrProt, SeqLim* pSeqLim, SeqExpo* pSeqExpo);
virtual bool run (MrProt* pMrProt, SeqLim* pSeqLim, SeqExpo* pSeqExpo, sSLICE_POS* pSLC);
protected:
private:
long TI1; //in us
long TI2; //in us
SBBList* pSBBList2;
SeqBuildBlockIRns myIRns_3(pSBBList2); // Line 106
};
bool MySeqBuildBlockModule::prep(MrProt* pMrProt, SeqLim* pSeqLim, SeqExpo* pSeqExpo)
{
NLS_STATUS lStatus;
double dEnergyAllSBBs_DK = myIRns_3.getEnergyPerRequest(); //Line 113
// Now we prepare:
lStatus = pSBBList->prepSBBAll(pMrProt, pSeqLim, pSeqExpo, &dEnergyAllSBBs_DK); // Line 116
if (lStatus ) {
cout << "DK: An error has occurred while preparing SBBs"<< endl;
}
return lStatus;
}
I would have like to intiantiate an object "myIRns_3" of a class defined in third party library
SeqBuildBlockIRns myIRns_3(pSBBList2);
and would like to access it from the prep function as:
double dEnergyAllSBBs_DK = myIRns_3.getEnergyPerRequest();
I tried to instantiate following in either private section or in constructor; but w/o any success:
SeqBuildBlockIRns myIRns_3(pSBBList2);
ERRORS encountered:
When I tried to do it inside the constructor, I get the following errors:
MySBBModule.h(113) : error C2065: 'myIRns_3' : undeclared identifier
MySBBModule.h(113) : error C2228: left of '.getEnergyPerRequest' must have class/struct/union type
MySBBModule.h(116) : error C2065: 'pSBBList' : undeclared identifier
MySBBModule.h(116) : error C2227: left of '->prepSBBAll' must point to class/struct/union
When I tried to do it in private section, I get the following errors:
MySBBModule.h(106) : error C2061: syntax error : identifier 'pSBBList2'
MySBBModule.h(113) : error C2228: left of '.getEnergyPerRequest' must have class/struct/union type
MySBBModule.h(116) : error C2065: 'pSBBList' : undeclared identifier
MySBBModule.h(116) : error C2227: left of '->prepSBBAll' must point to class/struct/union
PS: I have marked the line no in the code that I am posting.
Any help would be appreciated.
Regards,
DK
Your problem is that the below is declaring a function:
SeqBuildBlockIRns myIRns_3(pSBBList2);
What you want to do is declare a member:
SeqBuildBlockIRns myIRns_3;
And then in your constructor construct it with the argument pSBBList2:
MySeqBuildBlockModule (SBBList* pSBBList0, long TI1_In, long TI2_In) // more arguements in this constructor of derived class
: SeqBuildBlock (pSBBList0),
myIRns_3(pSBBList2) // <- construct
{ }