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.
Related
and I have the following error : "/clang:-1: linker command failed with exit code 1 (use -v to see invocation)"
the problem source is this :
I have a header file named "Action.h" :
#ifndef ACTION_H_
#define ACTION_H_
#include <string>
#include <iostream>
#include "Customer.h"
enum ActionStatus{
PENDING, COMPLETED, ERROR
};
//Forward declaration
class Restaurant;
class BaseAction{
public:
BaseAction();
ActionStatus getStatus() const;
virtual void act(Restaurant& restaurant)=0;
virtual std::string toString() const=0;
protected:
void complete();
void error(std::string errorMsg);
std::string getErrorMsg() const;
private:
std::string errorMsg;
ActionStatus status;
};
class Order : public BaseAction {
public:
Order(int id);
void act(Restaurant &restaurant);
std::string toString() const;
private:
const int tableId;
};
#endif
and when I try to implement the class baseAction and the derived class Order :
#include <stdio.h>
#include "Action.h"
using namespace std;
BaseAction::BaseAction(): errorMsg(""),status(PENDING) {}
ActionStatus BaseAction:: getStatus() const { return status;}
void BaseAction::complete() { status=COMPLETED;}
void BaseAction::error(string errorMsg) { this->errorMsg=errorMsg;}
string BaseAction::getErrorMsg() const { return this->errorMsg;}
Order::Order(int id): tableId(0) { }
I get the error. just to clear the error is caused only by this line :
Order::Order(int id): tableId(0) { }
*the .cpp file does recognize the header file, so this I reckon not the problem.
thank you very much !
in a MSVC dll project, i tried to create a dll containing a base class
//header file
class MATHLIBRARY_API Shape {
protected:
int width, height;
public:
Shape(int, int);
int area(void) { return -1; };
};
it's compiled successfully, but when adding a virtual specifier to the function
class MATHLIBRARY_API Shape {
protected:
int width, height;
public:
Shape(int, int);
virtual int area(void) { return -1; };
};
the compiler showed error msg
Error LNK2019 unresolved external symbol `__declspec(dllimport) const Shape::`vftable'" (__imp_??_7Shape##6B#) referenced in function "public: __thiscall Shape::Shape(int,int)" (??0Shape##QAE#HH#Z) Dll3 c:\Users\langj\source\repos\Dll3\Dll3\Dll3.obj 1
where could be the problem?
Today I've got a strange link error under VS 2012. When I tried to build my solution after some coding I got link error (LNK2001). When I canceled changes just by commenting added lines, the project builds. With some tries I got a strange thing. (next lines describes my actions in actual order)
When I build project with all lines, I'm getting LNK2001.
When I comment first constructor for GeneticNetworkRealisation - I'm getting LNK2001.
When I comment second constructor for GeneticNetwork - error disappears.
But when I return this line back to code - there is still no error.
So, this means that two identical code has different link success. Any ideas of how to fix this? Simplified code and errors:
NeuralNetwork.h
#pragma once
#include "Neuron.h"
#include "NeuralLayer.h"
struct Topology {
// some variables
};
class NeuralNetwork {
public:
NeuralNetwork(const Topology & topology);
NeuralNetwork(const NeuralNetwork & nn) : layers(nn.layers) {}
virtual ~NeuralNetwork() {}
protected:
std::vector<NeuralLayer> layers;
};
GeneticNetwork.h
#pragma once
#include "NeuralNetwork.h"
class GeneticNetwork : public NeuralNetwork {
public:
GeneticNetwork(const Topology & topology) : NeuralNetwork(topology) {}
GeneticNetwork(const GeneticNetwork & gnn) : NeuralNetwork(gnn) {}
virtual std::vector<double> getNetworkGeneticCode() const = 0;
virtual void mutate() = 0;
virtual NeuralNetwork crossingOver(const NeuralNetwork & nn) const = 0;
virtual ~GeneticNetwork() {}
};
GeneticNetworkRealisation.h
#pragma once
#include "GeneticNetwork.h"
#include "URNG.h"
struct MutationParameters {
// some parameters
};
const MutationParameters DEFAULT_MUTATION_PARAMS = {/*...*/};
class GeneticNetworkRealisation : public GeneticNetwork {
public:
GeneticNetworkRealisation(const GeneticNetworkRealisation & gnn) :
GeneticNetwork(gnn), random(gnn.random), mutationParams(gnn.mutationParams) {}
GeneticNetworkRealisation(const Topology & topology, URNG urng = URNG(),
const MutationParameters & mutationParameters = DEFAULT_MUTATION_PARAMS) :
GeneticNetwork(topology), random(urng), mutationParams(mutationParameters) {}
virtual std::vector<double> getNetworkGeneticCode() const override;
virtual void mutate() override;
virtual NeuralNetwork crossingOver(const NeuralNetwork & nn) const override;
virtual ~GeneticNetworkRealisation() {}
protected:
URNG & random;
MutationParameters mutationParams;
};
GeneticNetworkRealisation.cpp
#include "GeneticNetworkRealisation.h"
void GeneticNetworkRealisation::mutate() {
// code
}
std::vector<double> GeneticNetworkRealisation::getNetworkGeneticCode() const {
// code
}
NeuralNetwork GeneticNetworkRealisation::crossingOver(const NeuralNetwork & nn) const {
// code
}
Errors:
1>main.obj : error LNK2001: unresolved external symbol ""public: virtual class std::vector<double,class std::allocator<double> > __thiscall GeneticNetworkRealisation::getNetworkGeneticCode(void)const " (?getNetworkGeneticCode#GeneticNetworkRealisation##UBE?AV?$vector#NV?$allocator#N#std###std##XZ)"
1>main.obj : error LNK2001: unresolved external symbol ""public: virtual void __thiscall GeneticNetworkRealisation::mutate(void)" (?mutate#GeneticNetworkRealisation##UAEXXZ)"
1>main.obj : error LNK2001: unresolved external symbol ""public: virtual class NeuralNetwork __thiscall GeneticNetworkRealisation::crossingOver(class NeuralNetwork const &)const " (?crossingOver#GeneticNetworkRealisation##UBE?AVNeuralNetwork##ABV2##Z)"
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
};
class CommandManager {
public:
void sendText(std::string command);
static bool CommandManager::started;
private:
bool parseCommand(std::string commands);
void changeSpeed(std::vector<std::string> vec);
void help(std::vector<std::string> vec);
};
And here's the client code:
CommandManager::started = true;
Linking these two files together I get:
1>UAlbertaBotModule.obj : error LNK2001: unresolved external symbol "public: static bool CommandManager::started" (?started#CommandManager##2_NA)
1>C:\Development\School\cmput350-uofabot\UAlbertaBot\vs2008\Release\UAlbertaBot.dll : fatal error LNK1120: 1 unresolved externals
Where did I go wrong here?
You're doing that incorrectly.
class CommandManager {
public:
void sendText(std::string command);
static bool started; //NOT this -> bool CommandManager::started
//...
};
then put the definition of static member in .cpp file as:
#include "CommandManager.h" //or whatever it is
bool CommandManager::started = true; //you must do this in .cpp file
Now you can use CommandManager::started in your client code.
You should have inside your class:
class CommandManager {
public:
void sendText(std::string command);
static bool started;
//// etc
};
and outside your class, in a *.cc file (not in a *.hh header file), a definition like
bool CommandManager::started;
BTW, I believe you'll better make that private.
Consider putting
bool CommandManager::started;
where you define other members.