c++ VS-2019 Linker errror trying to link standalone class implementation - c++

I am working in C++ using VS-19 and having some linker issues I do not understand. I am coming from Java so I am not deep on building and linking plus I am working with kind of "legacy" code here so there might be things I don't grasp at all. I am getting this same error with multiple functions and classes but I am describing only one example here.
So I have a main function which calls the checkConnection function from the same file. Which in turn is referencing a class (myIp) defined in a different file which is in a different folder. When trying to call a function on the myIp object I get a linker error:
error LNK2019: unresolved external symbol "public: int __thiscall myIp::Connect(class AnsiString,int,unsigned int)" (?Connect#TIP##QAEHVAnsiString##HII_N#Z) referenced in function "int __cdecl checkConnection(void)" (?checkConnection##YAHXZ)
Folder Structure:
My main File: ..\projects\c++\ATT\MyApp--> here is all the stuff VS created when I started my new Solution
The myip File to include: ..\projects\c++\utilities--> here is the myIp.h and myIp.cpp files
This is what I have in my VS\Project Properties\VC++ Directories\Include Directories: ..\projects\c++\utilities
The myIp files are not part of a project or solution bound to VS just standalone class implementation.
This is my main code:
#include <myIp.h>
int checkConnection(AnsiString host, int url_port)
{
myIp conn;
if (conn.Connect(host, url_port, 10000))
{
// do magic
}
return 0;
}
int main()
{
AnsiString host = "http://10.20.30.40";
int port = 80;
return checkConnection(host, port);
}
This is the function declaration in myIp.h:
#ifndef classMYIPH
#define classMYIPH
class myIp
{
public:
myIp();
~myIp();
int Connect(AnsiString ipadr, int port, unsigned int
timeoutms=4000, unsigned int tries=1, bool event = false);
};
This is the function I am calling in myIp.cpp:
int myIp::Connect(AnsiString host, int port, unsigned int timeoutms, unsigned int tries, bool event)
{
// do connect
// if connection successfull return 1 else 0
}

myIp(); ~myIp(); lack definitions and you need to complete code or comment them.

Related

Compiler Error __ZTVN13..6..E

I'm currently struggeling with a compilerproblem. The problem is, that i use one of the MoSync example apps called "European Countries" (written in c++) to write my own. But when i compile the modified code, it gives me following error in response:
Controller.cpp:24: Error: Unresolved symbol '__ZTVN13Flightmanager6FlightE',
I already had a look at the example several times and i already copied the code from the example to mine, but it doesn't solve any problems.
In paticutlar i might understand what the error means (i do have c experience), but i've never seen such structured error. I also looked at namespacing conventions but there shouldn't be any problems.
//Flight.h
namespace Flightmanager
{
class Flight
{
public:
static int flightCounter;
/**
* The constructor creates the user interface.
*/
Flight(char *flightnumber, char *gate, char *departure, char *additionalinfo, char *destinationairport, char *destinationairportshort) {
this->_id = flightCounter;
flightCounter ++;
this->_flightnumber = flightnumber;
this->_gate = gate;
this->_departure = departure;
this->_additionalinfo = additionalinfo;
this->_destinationairport = destinationairport;
this->_destinationairportshort = destinationairportshort;
}
virtual ~Flight();
}
//Controller.h
#include [all other includes]
#include "../Model/Flight.h"
namespace Flightmanager
{
Controller::Controller():
mFlightArray(NULL),
mCurrentlyShownScreen(NULL)
{
initScreenSizeConstants();
initPlatformType();
//error: Unresolved symbol '__TZVN13Flightmanager6FlightE'.
initData();
//error: Unresoled symbol '__TZVN13Flightmanager6Flight13flightCounterE'.
mFlightTableView = new TableViewController(*this);//error: Unresoled symbol '__TZVN13Flightmanager6Flight13flightCounterE'.
mFlightDetailView = new DetailViewController();
}
}
I use MoSync Version 3.2
Build date: 121219-1556
Thx
You need to link in something that has definitions for:
Flight::flightCounter
Flight::~Flight()
whether that's a .o object file for Flight.cpp (or some source file) or a library depends on your project.

boost function and bind external symbol

I need to use bind and function in my program.But unfortunately vs2010 can't link my program.
I used following example from boost::bind documentation
#include <boost\bind.hpp>
#include <boost\function.hpp>
#include <functional>
class button
{
public:
boost::function<void()> onClick;
};
class player
{
public:
void play();
void stop();
};
button playButton, stopButton;player thePlayer;
void connect()
{
playButton.onClick = boost::bind(&player::play, &thePlayer);
stopButton.onClick = boost::bind(&player::stop, &thePlayer);
}
void main(int argc, char* argv[])
{
connect();
}
Error 1 error LNK2019: unresolved external symbol "public: void __thiscall player::stop(void)" (?stop#player##QAEXXZ) referenced in function "void __cdecl connect(void)" (?connect##YAXXZ)
i have tried the newest 32 and 64 verion of BoostPro and followed this tutorial http://www.youtube.com/watch?v=5AmwIwedTCM.All but vs still produces same error...
VS2010 project setting include/lib path
https://dl.dropbox.com/u/47585151/vs.png
however when I turned on
Linker->General->ShowProgress ->For Libraries Searched (/VERBOSE:Lib)
i noticed that VS is searching only for these libraries which are defined in
Linker->Input->Additional Dependencies
http://pastebin.com/BCpEt8Zq
is it possible to check which .lib boost need for boost::bind and boost::function under vs2010?
This problem has nothing to do with any boost library (both are header-only). Try to simply call start and stop from within connect and you should get the same error. Read it carefully, it tells you what is missing.

C++ error LNK2001 issue

I am fairly new to cpp but have been in c# for a while. I am trying to run a simple console application but I receive this LNK2001 error message.
I have the main.cpp, and have added another class, Zeus, with files, Zeus.h and Zeus.cpp.
Here is the main.cpp:
#include "Zeus.h"
#include <iostream>
int main()
{
Zeus::tick = 25.0;
using std::cout;
cout << "nothing";
}
Here is the Zeus.h:
static class Zeus
{
public:
static void testing(void);
public:
static double tick;
};
And here is the Zeus.cpp:
void Zeus::testing(void)
{
//Doesnt get this far
//But eventually something like
// cout << "test " << Zeus::tick;
}
And here is the error message:
Error 20 error LNK2001: unresolved external symbol "public: static double Zeus::tick"
Thanks,
You need to define Zeus::tick, typically you would to that in the in the Zeus.cpp file. You have only declared it.
double Zeus::tick = 0.0;
Also, there is no static class in C++.
As an aside, free functions can be put in namespaces, as opposed to being static functions of classes. This is the preferred way in C++, unless there are strong reasons for the function to be static.
namespace Dionysus {
void testing();
}
As the error message says: there's no definition of Zeus::tick. Add this to Zeus.cpp:
double Zeus::tick;
Oh, and in Zeus.h remove the static from
static class Zeus
In the main() function you have, what do you mean by a statement Zeus::tick = 25.0;?
Zeus is a class. So to access the individual elements of it you need to create its instance. Its just like a structure where you first create its instance to access its individual elements.
Try the following:
int main() {
Zeus myobject;
myobject.tick = 25.0;
/* Rest of the definition */
}

pass Enumeration to function in c++

i am really confusing about how to pass an enum type to a function in c++.
i am googling and test all the soloutions presented but non of them solve my problem.
in the SocketInfo.h ,
i have an enum named SocketType that declared globally as :
typedef enum SocketTypeEnum
{
SOCKET_TYPE_IPSEC
} SocketType;
in the SocketInfo.h i have a class named SocketInfo :
class SocketInfo
{
public:
SocketInfo(const char* ip,unsigned short fd,SocketType stype);
}
in the SocketInfo.cpp :
SocketInfo::SocketInfo(const char* ip, unsigned short fd,SocketType stype)
{
//some work done here
}
i build this class without any error
now for test this class i create a win32 console application.in the _tmain i write this code
#include "SocketInfo.h"
void Test_Socket()
{
SocketInfo* si = new SocketInfo(NULL,5060,SOCKET_TYPE_IPSEC);
}
int _tmain(int argc, _TCHAR* argv[])
{
Test_Socket();
getch();
return 0;
}
after running above code i got these errors:
Error 4 error LNK2019: unresolved external symbol "public: __thiscall SocketInfo::SocketInfo(char const *,unsigned short,enum SocketTypeEnum)" (??0SocketInfo##QAE#PBDGW4SocketTypeEnum###Z) referenced in function "void __cdecl Test_Socket(void)" (?Test_Socket##YAXXZ)
Error 5 error LNK1120: 1 unresolved externals
how i can solve these errors.
all codes compile on visual studio 2010 Ultimate.
The problem isn't in how you pass the enum as parameter, which is ok, but that you don't export symbols from your project or import them in your test project.
You need to add the lib file generated by the project defining SocketInfo to the additional dependencies of the test project.
You also need to export the class:
_declspec(dllexport) class SocketInfo
{
public:
SocketInfo(const char* ip,unsigned short fd,SocketType stype);
};
and import it in the test project with _declspec(dllimport). This duality is usually achieved through macros - look it up.

c++ LNK2001: unresolved external symbol problem

Greetings.
I have searched for a solution, but I think this problem is personal code specific, hence my posting here.
I'll get straight to the point.
In my main I have two objects.
Computer *computer = new Computer();
Player *player = new Player();
In the computer class, in the header I have the following:
private:
Strategy *strategy;
int winningPosition;
int twoInRow;
int counter;
int makeTwo;
Then in Computer.cpp:
Computer::Computer(char token, bool hasTurn)
{
m_token = token;
m_hasTurn = hasTurn;
strategy = new Strategy();
}
int Computer::getMove(const char (&board)[9])
{
twoInRow = strategy->checkTwoInRow(board);
counter = strategy->counter(board);
makeTwo = strategy->makeTwo(board);
if(twoInRow != 0)
{
return twoInRow - 1;
} else if(counter != 0) {
return counter - 1;
} else if(makeTwo != 0) {
return makeTwo - 1;
} else {
return 0;
}
}
At this point I think the problem arises.
The methods called from the class Strategy all require knowledge of the board thus:
int checkTwoInRow(const char (&board)[9]);
int counter(const char (&board)[9]);
int makeTwo(const char (&board)[9]);
The problems im getting, unabling them to compile:
Error 1 error LNK2019: unresolved external symbol "public: int __thiscall Strategy::makeTwo(char const (&)[9])" (?makeTwo#Strategy##QAEHAAY08$$CBD#Z) referenced in function "public: int __thiscall Computer::getMove(char const (&)[9])" (?getMove#Computer##QAEHAAY08$$CBD#Z) C:\CPP\TTT\Computer.obj tictactoeCPP
Error 2 error LNK2019: unresolved external symbol "public: int __thiscall Strategy::counter(char const (&)[9])" (?counter#Strategy##QAEHAAY08$$CBD#Z) referenced in function "public: int __thiscall Computer::getMove(char const (&)[9])" (?getMove#Computer##QAEHAAY08$$CBD#Z) C:\CPP\TTT\Computer.obj tictactoeCPP
Error 3 error LNK2019: unresolved external symbol "public: int __thiscall Strategy::checkTwoInRow(char const (&)[9])" (?checkTwoInRow#Strategy##QAEHAAY08$$CBD#Z) referenced in function "public: int __thiscall Computer::getMove(char const (&)[9])" (?getMove#Computer##QAEHAAY08$$CBD#Z) C:\CPP\TTT\Computer.obj tictactoeCPP
As a c++ noob, I have absolutely no clue why or how this problem is caused. I think it has to do something with either the instantiation of Strategy in the computer class, or with the parameter given from computer to the strategy in the method calls.
Can anyone explain WHY this error is occuring, I don't quite understand the error at all.
And also how to solve/prevent this?
EDIT*
I just got a request to share the Strategy class:
Strategy.h:
#pragma once
class Strategy
{
public:
Strategy(void);
~Strategy(void);
int checkTwoInRow(const char (&board)[9]);
int counter(const char (&board)[9]);
int makeTwo(const char (&board)[9]);
};
The class defined these methods, I won't post them because they are quite long.
This is a linking error and it has nothing to do with instantiations or parameters.
You haven't provided the linker with the definitions for those functions. If you defined them in Strategy.cpp you need to compile that and add it as an argument to the linker. How you do that depends entirely on what tools you're using to build your program.
If you're using Visual Studio (or any other IDE) it should take care of it automatically once you've added Strategy.cpp to your project.
Or did you perhaps define them like this:
int checkTwoInRow(const char (&board)[9])
{
// Do something with board the wrong way
}
instead of like this:
int Strategy::checkTwoInRow(const char (&board)[9])
{
// Do something with board the right way
}
The first one doesn't define a Strategy member function, it defines a global function.
The error is simply stating that you have declared, but not defined, the member functions Strategy::makeTwo, Strategy::counter, and Strategy::checkTwoInRow. Are you sure that you implemented them (in a source file that's actually being compiled) and that you didn't accidentally define them as free functions?