error calling vc++ method error LNK2019: unresolved external symbol referenced in function _wmain - visual-studio-2010

shows error calling vc++ 2010 express sp1 method of a object:
error LNK2019: unresolved external symbol "public: int __thiscall
oper::ShowMyResult(void)" (?ShowMyResult#oper##QAEHXZ) referenced in
function _wmain
ooad_oops_exer.h
#include "stdafx.h"
class oper {
public:
void DisplayDefault( );
void DisplayWidth( int n );
void DisplayLongs( );
int ShowMyResult();
};
ooad_oops_exer.cpp
#include "ooad_oops_exer.h"
//
#ifndef SOCKET_CLIENT_CLASS
#define SOCKET_CLIENT_CLASS
const double d1 = 1.23456789;
const long l1 = 16;
int base = 10;
void oper::DisplayDefault( )
{
cout << endl << "default display" << endl;
}
void oper::DisplayWidth( int n )
{
cout << endl << "fixed width display set to " << n << ".\n";
}
void oper::DisplayLongs( )
{
cout <<endl<<"hello";
}
int oper::ShowMyResult()
{
DisplayWidth(15);
DisplayDefault( );
cout <<endl<<"ShowMyResult";
return 0;
}
//
#endif
main.cpp
#include "stdafx.h"
#include "ooad_oops_exer.h"
int _tmain(int argc, _TCHAR* argv[])
{
oper op;
int k = op.ShowMyResult();
cin.get();
return 0;
}
EDIT
1> Generating Code... 1>Debug\ooad_oops_exer.obj : warning LNK4042:
object specified more than once; extras ignored 1>main.obj : error
LNK2019: unresolved external symbol "public: int __thiscall
oper::ShowMyResult(void)" (?ShowMyResult#oper##QAEHXZ) referenced in
function _wmain 1>C:\xxx\OOAD_OOPS_PROJ\Debug\OOAD_OOPS_PROJ.exe :
fatal error LNK1120: 1 unresolved externals 1>

Related

MFC project has linking error on Visual Studio 2013?

When I tried to add a toolbar to my mfc project in Visual studio 2013, the following errors occurred. Has anyone worked on this? How can I fix the errors?
mfc project in Visual studio 2013
MFCModalOynaDlg.h
#pragma once
class CMFCModalOynaDlg : public CDialogEx
{
public:
CMFCModalOynaDlg(CWnd* pParent = NULL);
enum { IDD = IDD_MFCMODALOYNA_DIALOG };
protected:
virtual void DoDataExchange(CDataExchange* pDX);
protected:
HICON m_hIcon;
CToolBar m_wndToolBar;
BOOL butD;
virtual BOOL OnInitDialog();
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnBnClickedOk();
};
MFCModalOynaDlg.cpp:
#include "stdafx.h"
#include "MFCModalOyna.h"
#include "MFCModalOynaDlg.h"
#include "afxdialogex.h"
#include "windows.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
CMFCModalOynaDlg::CMFCModalOynaDlg(CWnd* pParent /*=NULL*/)
: CDialogEx(CMFCModalOynaDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MYICON1);
}
void CMFCModalOynaDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CMFCModalOynaDlg, CDialogEx)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
END_MESSAGE_MAP()
BOOL CMFCModalOynaDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
SetIcon(m_hIcon, TRUE);
SetIcon(m_hIcon, FALSE);
if (!m_wndToolBar.Create(this)|| !m_wndToolBar.LoadToolBar(IDR_TOOLBAR1))
{
TRACE0("Failed to Create Dialog Toolbar\n");
EndDialog(IDCANCEL);
}
butD = TRUE;
CRect rcClientOld;
CRect rcClientNew;
GetClientRect(rcClientOld);
RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST, 0, reposQuery, rcClientNew);
CPoint ptOffset(rcClientNew.left - rcClientOld.left, rcClientNew.top - rcClientOld.top);
CRect rcChild;
CWnd* pwndChild = GetWindow(GW_CHILD);
while (pwndChild)
{
pwndChild->GetWindowRect(rcChild);
ScreenToClient(rcChild);
rcChild.OffsetRect(ptOffset);
pwndChild->MoveWindow(rcChild, FALSE);
pwndChild = pwndChild->GetNextWindow();
}
CRect rcWindow;
GetWindowRect(rcWindow);
rcWindow.right += rcClientOld.Width() - rcClientNew.Width();
rcWindow.bottom += rcClientOld.Height() - rcClientNew.Height();
MoveWindow(rcWindow, FALSE);
RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST, 0);
return TRUE;
}
Error 1 error LNK2001: unresolved external symbol "protected: void
__thiscall CMFCModalOynaDlg::OnPaint(void)" (?OnPaint#CMFCModalOynaDlg##IAEXXZ)
Error 2 error LNK2001: unresolved external symbol "protected: struct HICON__ * __thiscall CMFCModalOynaDlg::OnQueryDragIcon(void)" (?OnQueryDragIcon#CMFCModalOynaDlg##IAEPAUHICON__##XZ)
Error 3 error LNK1120: 2 unresolved externals D:\VSprogram\MFCModalOyna\Debug\MFCModalOyna.exe
When I tried to add a toolbar to my mfc project in Visual studio 2013, the following errors occurred.

Visual Studio cannot find my methods despite them being spelled correctly

I'm getting many unresolved symbol errors, and I can't figure out why.
The most common sources of this issue seem to be misspelled method names, forgetting to indicate what class the methods belong to using the scoping operator, and forgetting to include the .cpp file in the project.
I've checked all of these issues though, and none of them seem to apply. I'm telling it what class the methods belong to (via Point<T>:: ...), I've double checked the method names, and they're all correct, and the .cpp file is shown in the Solution Explorer (I even tried removing it and re-adding it).
I haven't written C++ in awhile, so I may be overlooking something, but I can't see what.
(After posting this, I realized my operators are comically broken. That shouldn't effect my error though, so please ignore for the time being).
Specifically, these are the errors I'm getting:
1>Main.obj : error LNK2019: unresolved external symbol "public: __thiscall Point<int>::Point<int>(int,int)" (??0?$Point#H##QAE#HH#Z) referenced in function _main
1>Main.obj : error LNK2019: unresolved external symbol "public: class Point<int> __thiscall Point<int>::operator+(class Point<int>)const " (??H?$Point#H##QBE?AV0#V0##Z) referenced in function _main
1>Main.obj : error LNK2019: unresolved external symbol "public: class Point<int> __thiscall Point<int>::operator-(class Point<int>)const " (??G?$Point#H##QBE?AV0#V0##Z) referenced in function _main
1>Main.obj : error LNK2019: unresolved external symbol "public: class Point<int> __thiscall Point<int>::operator*(class Point<int>)const " (??D?$Point#H##QBE?AV0#V0##Z) referenced in function _main
1>Main.obj : error LNK2019: unresolved external symbol "public: class Point<int> __thiscall Point<int>::operator/(class Point<int>)const " (??K?$Point#H##QBE?AV0#V0##Z) referenced in function _main
1>Main.obj : error LNK2019: unresolved external symbol "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class Point<int>)" (??6#YAAAV?$basic_ostream#DU?$char_traits#D#std###std##AAV01#V?$Point#H###Z) referenced in function _main
And these are the files in question:
Point.h
#ifndef POINT_H
#define POINT_H
#include <iostream>
template <typename T>
class Point {
const T x;
const T y;
public:
Point(T x, T y);
Point<T> moveBy(T xOff, T yOff) const;
Point<T> operator+(const Point<T> otherPoint) const;
Point<T> operator-(const Point<T> otherPoint) const;
Point<T> operator*(const Point<T> otherPoint) const;
Point<T> operator/(const Point<T> otherPoint) const;
friend std::ostream& operator<<(std::ostream& os, const Point<T> p);
private:
Point<T> applyOperator(T(*f)(T, T), const Point<T> otherPoint) const;
};
template <typename T>
std::ostream& operator<<(std::ostream& os, const Point<T> p) {
return os << "(" << p.x << "," << p.y << ")";
}
#endif
Point.cpp:
#include "Point.h"
template <typename T>
Point<T>::Point(T x, T y) :
x(x),
y(y) {
}
template <typename T>
Point<T> Point<T>::moveBy(T xOff, T yOff) const {
return this + Point(xOff, yOff);
}
template <typename T>
Point<T> Point<T>::operator+(const Point<T> otherPoint) const {
return applyOperator([](x, y) {x + y});
}
template <typename T>
Point<T> Point<T>::operator-(const Point<T> otherPoint) const {
return applyOperator([](x, y) {x - y});
}
template <typename T>
Point<T> Point<T>::operator*(const Point<T> otherPoint) const {
return applyOperator([](x, y) {x * y});
}
template <typename T>
Point<T> Point<T>::operator/(const Point<T> otherPoint) const {
return applyOperator([](x, y) {x / y});
}
template <typename T>
Point<T> Point<T>::applyOperator(T(*f)(T, T), const Point<T> otherPoint) const {
return Point(f(this.x, otherPoint.x), f(this.y, otherPoint.y));
}
And the Main.cpp:
#include <iostream>
#include "Point.h"
int main(int argc, char* argv[]) {
Point<int> p1(1, 2);
Point<int> p2(2, 3);
std::cout << p1 << std::endl;
std::cout << p2 << std::endl;
Point<int> p3 = p1 + p2;
Point<int> p4 = p1 - p2;
Point<int> p5 = p1 * p2;
Point<int> p6 = p1 / p2;
std::cout << p3 << std::endl;
std::cout << p4 << std::endl;
std::cout << p5 << std::endl;
std::cout << p6 << std::endl;
}
The problem is that the class template member function definitions are in Point.cpp, but when this file is compiled the compiler doesn't know that the template needs to be instantiated for T = int. In fact, I imagine Point.obj is essentially empty.
The compiler happily compiles Main.cpp with its calls to Point<int>'s members - expecting them to be provided at link time - but the linker then complains because it can't find those members defined anywhere.
The solution is to either (a) move Point's member function definitions into Point.hpp, or (b) force the instantiation of Point<int> in Point.cpp by explicitly declaring that specialisation: simply add the line template class Point<int>; at the end of Point.cpp.

Compiling a working 32bit .dll for 64bit, errors

I have with a lot of help put together a 32bit plugin (.dll) for the 32bit Teamspeak program which works great.
People are now asking me to compile this for the 64bit Teamspeak client.
But...........
When I do I get the following errors.
1>------ Build started: Project: GetFSComFreqs, Configuration: Release x64 ------
Creating library C:\Users\Roo\Desktop\pluginsdk\src\x64\Release\GetFSComFreqs.lib and object C:\Users\Roo\Desktop\pluginsdk\src\x64\Release\GetFSComFreqs.exp
GetFSComFreqs.obj : error LNK2019: unresolved external symbol SimConnect_Close referenced in function "void __cdecl DLLStop(void)" (?DLLStop##YAXXZ)
GetFSComFreqs.obj : error LNK2019: unresolved external symbol SimConnect_Open referenced in function "void __cdecl DLLStart(void)" (?DLLStart##YAXXZ)
SimConnectProcs.obj : error LNK2001: unresolved external symbol SimConnect_Open
GetFSComFreqs.obj : error LNK2019: unresolved external symbol SimConnect_CallDispatch referenced in function "void __cdecl DLLStart(void)" (?DLLStart##YAXXZ)
SimConnectProcs.obj : error LNK2019: unresolved external symbol SimConnect_AddToDataDefinition referenced in function "void __cdecl OnRecvOpen(struct SIMCONNECT_RECV_OPEN *,unsigned long,void *)" (?OnRecvOpen##YAXPEAUSIMCONNECT_RECV_OPEN##KPEAX#Z)
SimConnectProcs.obj : error LNK2019: unresolved external symbol SimConnect_RequestDataOnSimObject referenced in function "void __cdecl OnRecvOpen(struct SIMCONNECT_RECV_OPEN *,unsigned long,void *)" (?OnRecvOpen##YAXPEAUSIMCONNECT_RECV_OPEN##KPEAX#Z)
SimConnectProcs.obj : error LNK2019: unresolved external symbol SimConnect_GetNextDispatch referenced in function "unsigned int __cdecl MessageRoutine(void *)" (?MessageRoutine##YAIPEAX#Z)
C:\Users\Roo\Desktop\pluginsdk\src\x64\Release\GetFSComFreqs.dll : fatal error LNK1120: 6 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
A part of the Code...
#include "stdafx.h"
#define FSAPI __stdcall
HANDLE hSimConnect = 0;
HANDLE hMsgThread = 0;
HINSTANCE ThisModule = 0;
HRESULT hr = 0;
char ThisModuleName[MAX_PATH];
void FSAPI StartSimConnect()
{
if (NULL == hSimConnect)
{
GetModuleFileName(ThisModule, ThisModuleName, MAX_PATH);
hr = SimConnect_Open(&hSimConnect, ThisModuleName, NULL, NULL, NULL, 0);
hr = SimConnect_CallDispatch(hSimConnect, SimConnectDispatch, NULL);
if (FAILED(hr)) hSimConnect = 0;
else hMsgThread = (HANDLE)_beginthreadex(NULL, NULL, MessageRoutine, NULL, NULL, NULL);
}
}
void FSAPI StopSimConnect()
{
if (NULL != hSimConnect)
{
hr = SimConnect_Close(hSimConnect);
hSimConnect = 0;
}
}
int main() {}
void FSAPI DLLStart(void)
{
if (NULL == hSimConnect)
{
GetModuleFileName(ThisModule, ThisModuleName, MAX_PATH);
hr = SimConnect_Open(&hSimConnect, ThisModuleName, NULL, NULL, NULL, 0);
hr = SimConnect_CallDispatch(hSimConnect, SimConnectDispatch, NULL);
if (FAILED(hr)) hSimConnect = 0;
else hMsgThread = (HANDLE)_beginthreadex(NULL, NULL, MessageRoutine, NULL, NULL, NULL);
}
}
void FSAPI DLLStop(void)
{
if (NULL != hSimConnect)
{
hr = SimConnect_Close(hSimConnect);
hSimConnect = 0;
}
}
You are getting unresolved external symbols errors.
If all those SimConnect_* functions are coming from an external source, you need to link you program with the appropriate import library.
If you just changed the build configuration from win32 to x64, it might be the case that these import libraries were not changed into your x64 configuration.

RaceCar.obj : error LNK2019: unresolved external symbol "public: void __thiscall Car::setSpeed(int)" (?setSpeed#Car##QAEXH#Z)

im lost because i am copying straight out a book and i have went over it multiple times ad cant find a difference in my code and theirs. i need to learn what causes this problem so i can solve it in the future. the code keeps telling me there is something wrong with RaceCar
setSpeed somewhere
#include "stdafx.h"
#include
using namespace std;
class Car
{
protected:
bool isIgnitionOn;
int speed;
public:
void turnIgnitionOn();
void turnIgnitionOff();
void setSpeed(int);
void showCar();
};
void Car::showCar()
{
if(isIgnitionOn)
cout << " ignition is on" ;
else
cout << " ignition is off" ;
cout << "Speed is " << speed << endl;
}
void Car::turnIgnitionOn()
{
isIgnitionOn = true;
}
void Car::turnIgnitionOff()
{
speed = 0;
isIgnitionOn = false;
}
class RaceCar: public Car
{
public:
void setSpeed(int mph);
};
void RaceCar::setSpeed(int mph)
{
const int MAX_SPEED = 200;
if(isIgnitionOn)
if(mph<= MAX_SPEED)
speed = mph;
else
speed = MAX_SPEED;
else
cout << " cant set speed - ignition is off!" << endl;
}
int main()
{
Car myCar;
RaceCar aRaceCar;
myCar.turnIgnitionOn();
myCar.setSpeed(80);
cout << " car at 80 mph: " ;
myCar.showCar();
aRaceCar.turnIgnitionOn();
aRaceCar.setSpeed(80);
cout << " race car at 80 mph: ";
aRaceCar.showCar();
return 0;
}
Try making the the setSpeed(int) function in the Car class a virtual function. This will allow you to override the function in the RaceCar class.

boost::program_options overloaded validate is ambiguous

I am trying to parse a list input from the command line.
My class is derived from vector
The compiler complains about a overloaded validate being ambiguous.
I can see why, but do not know how to solve this issue.
Please help.
Below is a minimal example that generates the error. If the type of ch_list is changed to a vector, this minimal example compiles.
// g++ -std=c++11 -Wall -Wextra -pedantic test.cpp -o test -lboost_program_options -lboost_system
#include <vector>
#include <boost/program_options.hpp>
#include <iostream>
#include <vector>
using namespace std;
class mylist : public vector<int> {
public:
friend istream &operator>>(istream &is, mylist& l) {
int val;
is >> val;
l.push_back(val);
return is;
}
friend ostream& operator<<(ostream& os, const mylist& l) {
return os << l[0];
}
};
int main (int argc, char* argv[])
{
//vector<int> ch_list; // This works
mylist ch_list; // This doesn't
namespace po = boost::program_options;
po::options_description desc("Allowed options");
desc.add_options()
("ch", po::value<decltype(ch_list)>(&ch_list), "Set channel numbers")
;
po::variables_map vm;
try {
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);
}
catch (po::error& e) {
cerr << "ERROR: " << e.what() << "\n";
return EXIT_FAILURE;
}
if (!ch_list.empty())
cout << ch_list[0] << "\n";
return EXIT_SUCCESS;
}
I get these errors
/usr/include/boost/program_options/detail/value_semantic.hpp: In instantiation of ‘void boost::program_options::typed_value<T, charT>::xparse(boost::any&, const std::vector<std::basic_string<charT> >&) const [with T = mylist; charT = char]’:
test.cpp:47:5: required from here
/usr/include/boost/program_options/detail/value_semantic.hpp:169:13: error: call of overloaded ‘validate(boost::any&, const std::vector<std::basic_string<char> >&, mylist*, int)’ is ambiguous
/usr/include/boost/program_options/detail/value_semantic.hpp:169:13: note: candidates are:
/usr/include/boost/program_options/detail/value_semantic.hpp:81:10: note: void boost::program_options::validate(boost::any&, const std::vector<std::basic_string<charT> >&, T*, long int) [with T = mylist; charT = char]
/usr/include/boost/program_options/detail/value_semantic.hpp:129:10: note: void boost::program_options::validate(boost::any&, const std::vector<std::basic_string<charT> >&, std::vector<_RealType>*, int) [with T = int; charT = char]
You can use custom validator. Your code would be:
#include <vector>
#include <boost/program_options.hpp>
#include <iostream>
#include <vector>
using namespace std;
namespace po = boost::program_options;
class mylist : public vector<int> {
public:
};
void validate(boost::any& v,
const vector<string>& values,
mylist*, int) {
mylist dvalues;
for(vector<string>::const_iterator it = values.begin();
it != values.end();
++it) {
stringstream ss(*it);
copy(istream_iterator<int>(ss), istream_iterator<int>(),
back_inserter(dvalues));
if(!ss.eof()) {
throw ("Invalid coordinate specification");
}
}
v = mylist(dvalues);
}
int main (int argc, char* argv[])
{
//vector<int> ch_list; // This works
mylist ch_list; // This doesn't
po::options_description desc("Allowed options");
desc.add_options()
("ch", po::value< mylist >(&ch_list)->multitoken(), "Set channel numbers")
;
po::variables_map vm;
try {
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);
}
catch (po::error& e) {
cerr << "ERROR: " << e.what() << "\n";
return EXIT_FAILURE;
}
for (auto cc : ch_list)
cout << cc << endl;
return EXIT_SUCCESS;
}
Reference: boost::program_options config file option with multiple tokens

Resources