error LNK2001 inheritance problems - c++

I am working on a homework problem where we have to use inheritance. (I'm not very good with inheritance yet). We are to make a parent class "card_games" that has two children classes called "gofish" and "poker". We are given a template that our main has to follow, and the rest of the design is up to us. Here is my header file: (called "classes.h")
#include <vector>
using namespace std;
struct cards{
int rank;
char suit;
};
class players{
public:
int points;
int active;
vector<cards> cardhand;
void printhand(players *gameplayers, int person);
};
class card_games{
protected:
players *gameplayers;
void player_make();
public:
virtual void play();
};
class poker :public card_games{
public:
void play();
};
class gofish :public card_games{
public:
void play();
};
void player0_play(players *gameplayers, cards *cardlist, int people);
void createdeck(cards *cardlist);
void shuffle(cards *cardlist);
void deal(cards *cardlist, int people, players *gameplayers);
int getplayers();
I have determined the error is something to do with my virtual call. The error specifically is:
cardgames_play.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall card_games::play(void)" (?play#card_games##UAEXXZ)
And I believe this causes my next error of:
card_games.exe : fatal error LNK1120: 1 unresolved externals
Anyway something is wrong with my virtual void function.
Here is my main function:
#include <iostream>
#include "classes.h"
int main(){
card_games *game;
int opt;
game = NULL;
cout << "Poker 1, Go Fish 2" << endl;
cin >> opt;
if (opt == 1)
game = new poker;
else if (opt == 2)
game = new gofish;
game->play();
return 0;
}
We are supposed to roughly use this template. If I understand it correctly, I am creating an instance of Card_game class called game, then assigning game to an instance of either gofish or poker. Then I dereference "game" to the "play();" function. The rest of my code, I have a
void gofish::play(){
blah blah
}
and a
void poker::play(){
blah blah
}
Which have the rest of my code that works.
Any help regarding this error is greatly appreciated. Thanks!
P.S. I am using visual studio 2013 on windows 8.

The method void play(); in card_games does not have a body, and it is not pure virtual. Just do the change:
class card_games{
protected:
players *gameplayers;
void player_make();
public:
virtual void play()=0; //make it pure virtual
};

Related

C++ class as an argument of function not working [duplicate]

This question already has answers here:
Resolve build errors due to circular dependency amongst classes
(12 answers)
C++ circular include
(4 answers)
Closed 3 years ago.
I am working on a game, and right now I have followed a GameState manager tutorial but I'm not sure why isn't the code working. It might be because it's an older version of c++ in the tutorial but I can't seem to find how can I fix it.
#pragma once
#include "GameEngine.h"
class GameState
{
public:
virtual void Init() = 0;
virtual void Cleanup() = 0;
virtual void SplashScreen() = 0;
virtual void Pause() = 0;
virtual void Resume() = 0;
virtual void HandleEvents(GameEngine *game) = 0;
virtual void Update(GameEngine *game) = 0;
virtual void Draw(GameEngine *game) = 0;
void ChageState(GameEngine* game,
GameState* state) {
game->ChangeState(state);
}
protected: GameState() {}
};
If I don't game the GameEgine *game and GameState *state and remove every line that uses them my program works okay, my window appears but I can't change the game states which is really important.
If nobody has an answer that's okay :)
EDIT: I forgot to show the GameEngine.h file
#pragma once
#include "GameState.h"
#include "include.h"
class GameEngine
{
public:
Recources recource;
void Init(std::string name, int x, int y);
void Cleanup();
void SplashScreen();
void ChangeState(GameState* state);
void PushState(GameState* state);
void PopState();
void HandleEvents();
void Update();
void Draw(sf::RenderWindow &widnow);
bool Running() { return m_running; }
void Quit() { m_running = false; }
private:
// the stack of states
std::vector<GameState*> states;
bool m_running;
};
Variable game(which you pass to function) in ChangeState must be object GameEngine(which has function ChangeState,which is different from ChangeState of GameState), so you can call game->ChangeState, so if GameEngine object which you use have neccessary function and you pass correct arguments, all will be fine.

abstract class program providing LNK1169 error

I'm working on a cpp programm using abstract classes. Apparently the compiler doesn't accept the fact that I declared the abstract class and the derived. I have different files (.cpp and .h). The errors are the following:
1>IGeomObj.obj : error LNK2005: "public: virtual void __thiscall IGeomObj::circumference(void)" (?circumference#IGeomObj##UAEXXZ) already defined in GeoRect.obj
1>main.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall GeoRect::circumference(void)" (?circumference#GeoRect##UAEXXZ)
1>IGeomObj.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
And here the code:
main()
# include <iostream>
# include "IGeomObj.h"
# include "GeoCircle.h"
# include "GeoEllipse.h"
# include "GeoRect.h"
# include "GeoSquare.h"
# include "GeoTriangle.h"
using namespace std;
int main()
{
/*GeoSquare *R;
GeoTriangle *R;
GeoRect *R;
GeoRect *R;*/
int opt;
do{
cout<<endl<<"Menu:"<<endl<<endl<<"0. Exit"<<endl<<"1. New rectangle"
<<endl<<"2. New Square"<<endl<<"3. New Triangle"<<endl<<"4. New Circle"<<endl
<<"5. New Ellipse"<<endl;
cin>>opt;
switch(opt)
{
case 0:
break;
case 1:
GeoRect *R=new GeoRect;
cout<<endl<<"Height of the rectangle: ";
cin>>R->h;
cout<<endl<<"Width of the rectangle: ";
cin>>R->b;
R->output();
break;
};
}while(opt!=0);
system("pause");
return 0;
}
IGeomObj.h
#pragma once
#ifndef IGEOMOBJ_H
#define IGEOMOBJ_H
#include <iomanip>
#include <fstream>
class IGeomObj{
public:
float b,h,r,f,u;
virtual void output()=0;
virtual void area()=0;
virtual void circumference()=0;
};
#endif
IGeomObj.cpp
#include "IGeomObj.h"
#include <iostream>
using namespace std;
void IGeomObj::output(){};
void IGeomObj::area(){};
void IGeomObj::circumference(){};
GeoRect.h
#pragma once
#ifndef GEORECT_H
#define GEORECT_H
#include <iomanip>
#include <fstream>
#include "IGeomObj.h"
class GeoRect:public IGeomObj
{
public:
virtual void output();
virtual void area();
virtual void circumference();
};
#endif
GeoRect.cpp
#include "GeoRect.h"
#include <iostream>
using namespace std;
void GeoRect::output()
{
cout<<"Rectangle Area: "<<this->f<<" Circumference: "<<this->u;
};
void GeoRect::area()
{
this->f=this->h*this->b;
};
void IGeomObj::circumference()
{
this->u=2*this->h+2*this->b;
};
Your GeoRect.cpp file implements IGeomObj::circumference(), which is legal as you can provide an implementation for a pure virtual function.
However this is not sufficient are you're not also implementing GeoRect::circumference(), which is required on account of the base class function being declared as a pure virtual function.
In GeoRect.cpp you have a definition for IGeomObj::circumference.
void IGeomObj::circumference()
{
this->u=2*this->h+2*this->b;
};
but this should be a definition for GeoRect instead.
void GeoRect::circumference()
{
this->u=2*this->h+2*this->b;
};
It's also not necessary to add this-> for every reference to a member.
void GeoRect::circumference()
{
u=2*h+2*b;
};

error LNK2019: unresolved external symbol... referenced in function [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 7 years ago.
So I'm working on serialization (I have never done it before so this is a first for me) and what I have done is created a base class called serialisable that all my other classes that can serialize can inherent from:
#include <iostream>
class Serializable{
public:
Serializable();
virtual ~Serializable();
virtual void serialize();
virtual void deserialize();
};
I then have a class that inherits from it which is my AbstractChunk class:
#pragma once
#include <iostream>
#include <list>
#include <fstream>
#include "AbstractBlock.h"
#include "Serialisable.h"
using namespace std;
#ifndef ABSTRACTCHUNK_H
#define ABSTRACTCHUNK_H
class AbstractChunk: public Serializable{
public:
AbstractChunk();
AbstractChunk(int x, int y);
~AbstractChunk();
virtual int getXpos();
virtual int getYpos();
virtual bool unload();
void serialize();
void deserialize();
private:
list<AbstractBlock> blocks;
int xpos;
int ypos;
};
#endif
and then the .cpp for my AbstractChunk (I edited out all the non important stuff):
#include "AbstractChunk.h"
void AbstractChunk::serialize(){
ofstream chunkFile;
chunkFile.open("ChunkData/" + to_string(xpos) + "." + to_string(ypos) + ".chunk");
if (!chunkFile.good())
cout << "Problem Opening Chunk File" << xpos << "." << ypos << endl;
chunkFile << "xpos:" << xpos << "\n";
chunkFile << "ypos:" << ypos << "\n";
chunkFile.close();
}
void AbstractChunk::deserialize(){
}
So where is this error coming from? It's a linker error however I didn't mess with the dependencies or the project setup at all, I have a feeling I'm doing something stupid as usual.
EDIT
Here are the actual errors
Error 1 error LNK2019: unresolved external symbol "public: __thiscall Serializable::Serializable(void)" (??0Serializable##QAE#XZ) referenced in function "public: __thiscall AbstractChunk::AbstractChunk(int,int)" (??0AbstractChunk##QAE#HH#Z) C:\Users\Magnus\Documents\Visual Studio 2013\Projects\Top Down Shooter\Top Down Shooter\AbstractChunk.obj Top Down Shooter
Error 2 error LNK2019: unresolved external symbol "public: virtual __thiscall Serializable::~Serializable(void)" (??1Serializable##UAE#XZ) referenced in function __unwindfunclet$??0AbstractChunk##QAE#HH#Z$0 C:\Users\Magnus\Documents\Visual Studio 2013\Projects\Top Down Shooter\Top Down Shooter\AbstractChunk.obj Top Down Shooter
You are not specifying the exact linking error but for sure you are missing some methods, you have declared:
class Serializable {
..
virtual void serialize();
virtual void deserialize();
}
as non pure virtual methods, and you are not implementing them. You should turn them to pure, since Serializable doesn't implement functionality but it's only an interface:
class Serializable {
..
virtual void serialize() = 0;
virtual void deserialize() = 0;
}

Doesn't run errorLNK1120: 1 unresolved externals VS

I have these 2 simple classes. I have made a win32 app, but when i run it it shows me this error :
Error 2 error LNK1120: 1 unresolved
externals C:\Users\D\Documents\Visual Studio Projects\LR3
SYSPROG\Debug\LR3 SYSPROG.exe 1 1 LR3 SYSPROG
I have tried it to run before it worked just fine how did i screw it up even with out touching it ?
class Liquid
{
private:
char *name;
int density;
public:
void CLperenaznachenie();
//virtual void spiling();
public:
char* get_name() const;
void set_name(char const* new_name);
int get_density() const;
void set_density(int new_density);
};
class Alcohol:public Liquid
{
private:
int fortress;
public:
void CAperenaznachenie();
//void spiling(){
//new stuff
//};
public:
int get_fortress() const;
void set_fortress(int new_fortress);
};
I have solved it by making it a console app and adding a main() function and that is what i need.

error LNK2019 for unknown reason

My code is like this:
Basic.h
#define Type double
Model.h
#include "Basic.h"
class Model{
protected:
int _N;
public:
Model(int _N, Type* config){ config=new Type[N]; _N=N}
virtual Type compute();
}
class Model1: public Model{
protected:
int _Nr,_Nc;
public:
Model1(int Nr, int Nc, Type* config):Model(Nr*Nc,config){_Nr=Nr;_Nc=Nc;}
virtual Type compute();
}
class Model2: Public Model{
public:
Model2(int N,Type* config):Model(N,config){/*other unrelated codes*/}
virtual Type compute();
}
Model1.cpp
#include "Model.h"
Type Model1::compute(){
/*definition*/
}
Model2.cpp
#include "Model.h"
Type Model2::compute(){
/*definition*/
}
Method.h
#include "Model.h"
void Method(Model* sys);
Method.cpp
#include "Method.h"
void Method(Model* sys){
Type a=sys->compute();
/*code*/}
Main.cpp
#include "Method.h"
int main(int argc, char** argv){
Model* sys=new Model2();
Method(sys);
/*code*/
}
I can't find any problems but the compiler keeps complaining "error LNK2019: unresolved external symbol void __cdecl Method(class Model *) referenced in function _main".
I am so frustrated because I am a beginner of this and fail to find out the key. I don't know what can cause this: is it anything wrong in my #define? Or it is because different subclass have functions with the same name(seems it should be OK)? Or there is other bugs? I don't even know to add what tags to this question...
Can anyone help me to figure this out? Thanks a lot!
Thanks for suggestions and I have updated the questions to make sure all the constructors are contained.
It seems as if Method.cpp is not part of the project, so it's not compiled and the linker can't find Method.
You should add all CPP files to your project - Main, Method, Model1 and Model2. While you're at it, make sure the H files are all there, as well.
Missing some semicolons in the Model.h I think.
You need to end the class definitions with semicolon.
For example:
class Model{
protected:
int _N;
public:
Model(int _N, Type* config){ config=new Type[N]; _N=N}
virtual Type compute();
};
Not sure if this is the solution to your problem however. But missing semicolons can bring up all sorts of issues.