This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I have looked and I know there are other answers out there but none of them seem to give me what i'm looking for so please don't report this as a "repost"
I'm getting the unresolved external symbol "public: __thiscall" error in my C++ code and i'm about to kick it out the window and just fail my C++ class. Please help me!!!!
My checking account header file
#include "BankAccount.h"
class CheckingAccount
{
private:
int numOfWithdrawls;
double serviceFee;
int AccountBal;
public:
bool withdraw (double wAmmt);
BankAccount CA;
CheckingAccount();
CheckingAccount(int accountNum);
};
and its CPP file
#include <iostream>
using namespace std;
#include "CheckingAccount.h"
CheckingAccount::CheckingAccount()
{
CA;
numOfWithdrawls = 0;
serviceFee = .50;
}
CheckingAccount::CheckingAccount(int accountNum)
{
CA.setAcctNum (accountNum);
numOfWithdrawls = 0;
serviceFee = .50;
}
bool CheckingAccount::withdraw (double wAmmt)
{
numOfWithdrawls++;
if (numOfWithdrawls < 3)
{
CA.withdraw(wAmmt);
}
else
{
if (CA.getAcctBal() + .50 <=0)
{
return 0;
}
else
{
CA.withdraw(wAmmt + .50);
return 1;
}
}
}
My BankAccount header file
#ifndef BankAccount_h
#define BankAccount_h
class BankAccount
{
private:
int acctNum;
double acctBal;
public:
BankAccount();
BankAccount(int AccountNumber);
bool setAcctNum(int aNum);
int getAcctNum();
double getAcctBal();
bool deposit(double dAmmt);
bool withdraw(double wAmmt);
};
#endif
My BankAccount CPP file
#include <iostream>
using namespace std;
#include "BankAccount.h"
BankAccount::BankAccount(int AccoutNumber)
{
acctNum = 00000;
acctBal = 100.00;
}
bool BankAccount::setAcctNum(int aNum)
{
acctNum = aNum;
return true;
}
int BankAccount::getAcctNum()
{
return acctNum;
}
double BankAccount::getAcctBal()
{
return acctBal;
}
bool BankAccount::deposit(double dAmmt)
{
acctBal += dAmmt;
return true;
}
bool BankAccount::withdraw(double wAmmt)
{
if (acctBal - wAmmt <0)
{
return 0;
}
else
{
acctBal -= wAmmt;
return 1;
}
}
My error:
1>BankAccountMain.obj : error LNK2019: unresolved external symbol "public: __thiscall BankAccount::BankAccount(void)" (??0BankAccount##QAE#XZ) referenced in function "public: __thiscall SavingsAccount::SavingsAccount(void)" (??0SavingsAccount##QAE#XZ)
1>CheckingAccount.obj : error LNK2001: unresolved external symbol "public: __thiscall BankAccount::BankAccount(void)" (??0BankAccount##QAE#XZ)
The "__thiscall" is noise. Read on. The error messages are complaining about BankAccount::BankAccount(void). The header file says that BankAccount has a default constructor, but there's no definition for it.
In your BankAccount class you declare a constructor that takes no arguments
BankAccount();
but you do not implement it. This is why the linker cannot find it. Provide an implementation for this constructor in your .cpp file and the link step should work.
Related
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.
I'm a visual studio 2015 c++ newby who's trying to write some game code at home.
I'm getting this link error:
LNK2019 unresolved external symbol "public: class std::basic_string,class std::allocator > __thiscall display_utils::fit_int_2(int)" (?fit_int_2#display_utils##QAE?AV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##H#Z) referenced in function "public: void __thiscall bat_stats::disp_bat_stats(struct bat_stats::bat_stats_typ)" (?disp_bat_stats#bat_stats##QAEXUbat_stats_typ#1##Z)
It apparently doesn't like the string I'm using to access the returned string from function fit_int_2. I've google searched for a solution, but can't find anything that fixes my problem. Note that the code compiled and linked before i I added the fit_int_2 call. Thanks in advance if you can help me out. The code is below:
bat_stats.h
#pragma once
class bat_stats
{
public:
struct bat_stats_typ
{
int gm;
int ab;
int ht;
int dbl;
int trpl;
int hr;
int rbi;
int sb;
int cs;
int bb;
int ibb;
int sf;
int sac;
int k;
int gidp;
int err;
float ave;
float slg;
float obp;
};
void disp_bat_hdr();
void disp_bat_stats( bat_stats_typ );
private:
int dummy;
};
bat_stats.cpp
#include <iostream>
using std::cout;
std::cin;
#include <string>
using std::string;
#include "bat_stats.h"
#include "display_utils.h"
void bat_stats::disp_bat_hdr()
{
cout << " G AB H 2B 3B HR RBI SB CS BB IW SF SH K GDP E AVE SLG OBP\n";
}
void bat_stats::disp_bat_stats( bat_stats_typ bat )
{
display_utils dut;
string s;
s = dut.fit_int_2( bat.gm ); // <- the problem is here!
cout << s << bat.gm << " ";
cout << bat.ab << "\n\n";
}
display_utils.h
#pragma once
#include <string>
using std::string;
class display_utils
{
public:
void insert_5_lines();
string fit_int_2( int );
private:
int dummy;
};
display_utils.cpp
#include <iostream>
using std::cout;
#include "display_utils.h"
void display_utils::insert_5_lines()
{
cout << "\n\n\n\n\n";
}
string fit_int_2(int i0)
{
string s0 = "";
if (i0 < 10)
{
s0 = " ";
}
return s0;
}
You need to change
string fit_int_2(int i0)
to
string display_utils::fit_int_2(int i0)
(You need to define the member function - currently you're defining an unrelated global function.)
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 8 years ago.
I was just trying to see if I can read a text file and display but I have this error:
2 error LNK2019: unresolved external symbol "public: void __thiscall
WeatherReport::displayReport(void)"
(?displayReport#WeatherReport##QAEXXZ) referenced in function _main
Can anyone explain me what is causing this, why this is happening and how to fix this problem?
#include<fstream>
#include<iomanip>
#include<stdio.h>
#include<cmath>
#include<iostream>
using namespace std;
class WeatherReport
{
WeatherReport friend monthEnd(WeatherReport, WeatherReport);
private:
int dayofMonth;
int highTemp;
int lowTemp;
double amoutRain;
double amoutSnow;
public:
WeatherReport(int Day = 0);
void setValues(int, int, int, double, double);
void getValues();
void displayReport();
}
void WeatherReport::setValues(int dom, int ht, int lt, double ar, double as)
{
dayofMonth = dom;
highTemp = ht;
lowTemp = lt;
amoutRain = ar;
amoutSnow = as;
}
int main()
{
const int DAYS = 30;
WeatherReport day[DAYS];
WeatherReport summary;
int i = 0;
ifstream inFile;
inFile.open("WeatherTest.txt");
if (!inFile)
cout << "File not opended!" << endl;
else
{
int dom, ht, lt;
double ar, as;
while (inFile >> dom >> ht >> lt >> ar >> as)
{
day[i].setValues(dom, ht, lt, ar, as);
i++;
}
inFile.close();
for (int i = 0; i < DAYS; i++)
{
day[i].displayReport();
//read one line of data from the file
//pass the data to setValues to initialize the object
}
system("PAUSE");
return 0;
}
Your displayReport does not have a function body, and as such does not have an external symbol referencing it, hence the error.
Add a function body for displayReport, and the issue will go away:
void WeatherReport::displayReport()
{
//Place your code here.
}
The following code can be used to reproduce this error:
[header file- test.h]:
#include "StdAfx.h"
void someOtherFunction();
void someFunction(string thisVar);
[code file- test.cpp]:
#include "StdAfx.h"
#include "test.h"
void someOtherFunction()
{
printf("Hello World!");
}
[function body for someFunction(string thisVar) is missing!]
The error speaks for itself
LNK2019: unresolved external symbol "public: void __thiscall WeatherReport::displayReport(void)
It can't find the definition for WeatherReport::displayReport(). I see its declaration in your code, but there's no definition anywhere. Either you didn't write a definition, or you provided it and didn't link the .cpp file that it's in. I'm guessing the former.
Seems like displayReport() does not have a body - it is only declared, but not defined. Add the following:
void WeatherReport::displayReport()
{
//your code
}
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 8 years ago.
I'm a new guy in C++ and I could not understand where I am wrong in this code. I take this error:
ClCompile:
1> Student.cpp
1>Student.obj : error LNK2019: unresolved external symbol "public: void __thiscall Student::setExamGrade(int,int)" (?setExamGrade#Student##QAEXHH#Z) referenced in function _main
1>c:\users\administrator\documents\visual studio 2010\Projects\LAB1\Debug\LAB1.exe : fatal error LNK1120: 1 unresolved externals
1>
1>Build FAILED.
Could you please help me? Code here:
Student.h
#ifndef STUDENT_H
#define STUDENT_H
#include <string>
using namespace std;
class Student
{
private:
int ID;
string name;
int *exams;
public:
Student();
Student(int ID, string name);
void setExamGrade(int index, int grade);
int getOverallGrade();
void display();
};
#endif
Student.cpp
#include "Student.h"
#include <iostream>
using namespace std;
int total;
int count;
int average;
int exams[3];
void main() {
Student *s = new Student(123, "John");
s->setExamGrade(0, 80);
s->setExamGrade(1, 60);
s->setExamGrade(2, 95);
s->display();
delete s;
}
Student :: Student()
{
ID = 0;
name = "";
}
Student :: Student(int num, string text)
{
this->ID = num;
this->name = text;
}
void setExamGrade(int index, int grade)
{
exams[index] = grade;
total += exams[index];
count = index +1;
}
int getOverallGrade()
{
average = total/count;
return average;
}
void Student :: display()
{
cout << "ID:" << ID << "NAME:" << name << "GRADE:" << endl;
}
You declare the method:
void setExamGrade(int index, int grade);
Inside the class Student
But you don't define the method. You do define a function with the same name.
void setExamGrade(int index, int grade)
{ // STUFF
}
But that is not a method definition,
I think you missed the Student :: before setExamGrade and getOverallGrade.
You have it defined like so
void setExamGrade(int index, int grade) { .. }
That is just a function by itself, and it doesn't belong to a class. You want
void Student::setExamGrade(int index, int grade) { .. }
"unresolved external symbol" means the body of the code in question is not found by the linker.
In this case it's the Student::setExamGrade method whose body is not found.
Your code appears to have defined a function setExamGrade but this has not been flagged as a Student:: method (in the way that you have successfully done for Student::display)
I keep getting the same error messages every time on Visual Studio. I don't know where the error is originating. Basically I'm trying to convert an infix expression (A+B-C) to a postfix expression (AB+C-) using stacks. Any help would be greatly appreciated
DriverExpression.cpp
Expression.obj : error LNK2019: unresolved external symbol "public: __thiscall stackType::stackType(int)" (??0?$stackType#D##QAE#H#Z) referenced in function "public: void __thiscall Expression::convertInfixToPostfix(void)" (?convertInfixToPostfix#Expression##QAEXXZ)
Expression.obj : error LNK2019: unresolved external symbol "public: __thiscall stackType::~stackType(void)" (??1?$stackType#D##QAE#XZ) referenced in function "public: void __thiscall Expression::convertInfixToPostfix(void)" (?convertInfixToPostfix#Expression##QAEXXZ)
Expression.obj : error LNK2019: unresolved external symbol "public: char __thiscall stackType::top(void)const " (?top#?$stackType#D##QBEDXZ) referenced in function "public: void __thiscall Expression::convertInfixToPostfix(void)" (?convertInfixToPostfix#Expression##QAEXXZ)
Expression.obj : error LNK2019: unresolved external symbol "public: void __thiscall stackType::initializeStack(void)" (?initializeStack#?$stackType#D##QAEXXZ) referenced in function "public: void __thiscall Expression::convertInfixToPostfix(void)" (?convertInfixToPostfix#Expression##QAEXXZ)
Expression.obj : error LNK2019: unresolved external symbol "public: bool __thiscall stackType::empty(void)const " (?empty#?$stackType#D##QBE_NXZ) referenced in function "public: void __thiscall Expression::convertInfixToPostfix(void)" (?convertInfixToPostfix#Expression##QAEXXZ)
Expression.obj : error LNK2019: unresolved external symbol "public: void __thiscall stackType::push(char)" (?push#?$stackType#D##QAEXD#Z) referenced in function "public: void __thiscall Expression::convertInfixToPostfix(void)" (?convertInfixToPostfix#Expression##QAEXXZ)
Expression.obj : error LNK2019: unresolved external symbol "public: void __thiscall stackType::pop(void)" (?pop#?$stackType#D##QAEXXZ) referenced in function "public: void __thiscall Expression::convertInfixToPostfix(void)" (?convertInfixToPostfix#Expression##QAEXXZ)
C:\Users\Jake Bock\Desktop\CSC 240\Project 7\JBock_Project7\Debug\JBock_Project7.exe : fatal error LNK1120: 7 unresolved externals**
#include <iostream>
#include <fstream>
#include <string>
#include <stack>
#include "Expression.h"
#include "stackType.h"
using namespace std;
int main()
{
string fileName;
string infixExpression, postfixExpression;
Expression expression;
cout << "Converting infix expression to postfix expression\n"
<< "testing infix expressions are in input file\n";
cout << "Enter the input file name ";
cin >> fileName;
ifstream inData;
ofstream outData;
inData.open(fileName.c_str());
outData.open("out.txt");
cout<<"open="<<fileName<<endl; //debugging print
while(inData>>infixExpression)
{
cout<<"Infix="<<infixExpression<<endl; //debugging print
expression.setInfix(infixExpression);
cout<<"setInfix="<<infixExpression<<endl;//debugging print
expression.convertInfixToPostfix();
cout<<"convert="<<infixExpression<<endl; //debugging print
postfixExpression = expression.getPostfix();
cout << "Infix Expression: "
<< infixExpression <<"\n"
<< "Postfix Expression: "
<<postfixExpression<<"\n\n";
outData << "Infix Expression: "
<< infixExpression <<"\n"
<< "Postfix Expression: "
<<postfixExpression<<"\n\n";
}
inData.close();
outData.close();
system("pause");
return 0;
}
================================================================
#ifndef EXPRESSION_H_
#define EXPRESSION_H_
#include "stackType.h"
class Expression
{
private:
std::string infixExpression;
std::string postfixExpression;
bool precedence(char s, char operator2); //ToDo
bool isOperand(char symbol); //ToDo
public:
Expression();
void setInfix(std::string infix);
std::string getInfix();
std::string getPostfix();
void convertInfixToPostfix(); //ToDo
static void convertInfixToPostfix( //ToDo
std::string& infix,
std::string& postfix);
virtual ~Expression();
};
#endif /* EXPRESSION_H_ */
#include <stack>
#include <string>
#include <sstream>
#include "Expression.h"
#include "stackType.h"
#include <iostream>
using namespace std;
Expression::Expression() : infixExpression(""), postfixExpression(""){}
void Expression::setInfix(string infix)
{
infixExpression= infix;
}
string Expression::getInfix()
{
return infixExpression;
}
string Expression::getPostfix()
{
return postfixExpression;
}
void Expression::convertInfixToPostfix()
{
/*Define operator stack*/
stackType<char> s(infixExpression.size());
s.initializeStack();
ostringstream strstream;
int operand = 0;
/*String stream will help us pushing integer to our stream easily.
This is similar to sprintf(), but with less hassles.*/
int size = infixExpression.size();
int iter = 0;
for (; iter < size; ++iter)
{
switch (infixExpression[iter])
{
case '+':
case '-':
case '/':
case '*':
{
if (s.empty())
{
/*We always push the first operator*/
s.push(infixExpression[iter]);
}
else
{
/*if precedence of current operator is higher than
one on top of the stack, we simply push the current
operator to the top of the stack*/
if (precedence(s.top(), infixExpression[iter]))
{
s.push(infixExpression[iter]);
}
else
{
/*Pop from the stack and append it to string stream.
Repeat it unless we find an operator on top of the stack
whose precedence is lower or stack is empty.*/
do
{
strstream << s.top() << ' ';
s.pop();
}while (!s.empty() && !precedence(s.top(), infixExpression[iter]));
/*Now push the current operator to the stack*/
s.push(infixExpression[iter]);
}
}
break;
}
case ' ':
{
break;
}
default:
{
while (iter < size && infixExpression[iter] >= '0' && infixExpression[iter] <= '9')
{
operand = operand * 10 + (infixExpression[iter]-'0');
++iter;
}
strstream << operand << ' ';
operand = 0;
--iter;
break;
}
}
}
/*Pop one by one from operator stack and append it to our stream*/
while(!s.empty())
{
strstream << s.top() << ' ';
s.pop();
}
/*Get the string associated with our string stream*/
postfixExpression = strstream.str();
}
bool Expression::precedence(char s, char operator_specified)
{
if ((operator_specified == '+') || (operator_specified == '-'))
return false;
char top_operator = s;
if ((top_operator == '+') || (top_operator == '-'))
return true;
else
return false;
}
Expression::~Expression()
{
}
//=======================================================================================
#ifndef STACKTYPE_H_
#define STACKTYPE_H_
template <class T>
class stackType
{
private:
int maxStackSize;
int stackTop;
char *list;
void copyStack(const stackType& otherStack);
public:
stackType();
stackType(int stackSize);
~stackType();
char top() const;
void initializeStack();
bool empty() const;
void push(const char newItem);
bool isFullStack() const;
void pop();
const stackType& operator=(const stackType& otherStack);
};
#endif /* stackType_H_ */
#include "stackType.h"
#include <stack>
#include <cassert>
template <class T>
stackType<T>::stackType(){}
template <class T>
stackType<T>::stackType(int stackSize)
{
if (stackSize <= 0)
{
cout << "Size of the array to hold the stack must be positive" << endl;
cout << "Creating an array of size 100" << endl;
maxStackSize = 100;
}
else
maxStackSize = stackSize;
stackTop = 0;
list = new char[maxStackSize];
}
template <class T>
stackType<T>::~stackType()
{
delete [] list;
}
template <class T>
char stackType<T>::top() const
{
assert(stackTop != 0);
return list[stackTop - 1];
}
template <class T>
void stackType<T>::initializeStack()
{
stackTop = 0;
}
template <class T>
bool stackType<T>::empty() const
{
return (stackTop == 0);
}
template <class T>
bool stackType<T>::isFullStack() const
{
return (stackTop == maxStackSize);
}
template <class T>
void stackType<T>::pop()
{
if (!isEmptyStack())
stackTop--;
else
cout << "Cannot remove from an empty stack" << endl;
}
template <class T>
void stackType<T>::push(const char newItem)
{
if (!isFullStack())
{
list[stackTop] = newItem;
stackTop++;
}
else
cout << "Cannot add to a full stack" << endl;
}
template <class T>
void stackType<T>::copyStack(const stackType& otherStack)
{
delete [] list;
maxStackSize = otherStack.maxStackSize;
stackTop = otherStack.stackTop;
}
template <class T>
const stackType<T>& stackType<T>::operator=(const stackType& otherStack)
{
if (this != &otherStack)
copyStack(otherStack);
return *this;
}
unresolved external symbol
Okay, so you've got a linker error here. Whenever you get that, here's the process for fixing it. First, let's look at the error message.
public: __thiscall stackType::stackType(int)" (??0?$stackType#D##QAE#H#Z) referenced in function "public: void __thiscall Expression::convertInfixToPostfix(void)" (?convertInfixToPostfix#Expression##QAEXXZ)
Figure out what method isn't linking. Looking at the error message, it's clearly stackType::stackType(int).
Now what's happening is your method (in this case, a constructor) is declared but never defined. So, go through the checklist...
Is stackType.cpp being included into your build?
Did you remember to write stackType::stackType(int) in your .cpp file?
Are the method signatures for the two functions the same? Did you make one a const method and the other not?
Any weird spelling errors in the class or method name?
Are you doing anything wrong involving templates?
My guess is that #4 is the culprit here, since stackType is a template class. You need to stick all those method definitions into the header and probably aren't.
So the problem turned out being that I needed to keep the template class member function definitions in the header file only. Oy.
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 9 years ago.
I created Linked list and it has no problem and runs. Plus i have to include Binary Tree. and its giving me this error
1> Generating Code...
1> Skipping... (no relevant changes detected)
1> main.cpp
1>main.obj : error LNK2019: unresolved external symbol "public: int __thiscall SDI::BinaryTree::insert(int)" (?insert#BinaryTree#SDI##QAEHH#Z) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "public: int __thiscall SDI::BinaryTree::preOrder(void)" (?preOrder#BinaryTree#SDI##QAEHXZ) referenced in function _main
1>C:\Users\Muugii\Documents\Visual Studio 2013\LinkedList\Debug\LinkedList.exe : fatal error LNK1120: 2 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
BinaryTree.h
ifndef SDI_BINARYTREE
define SDI_BINARYTREE
include
namespace SDI
{
class BinaryTree
{
class BTNode
{
public:
int data;
BTNode *left;
BTNode *right;
};
public:
BinaryTree();
~BinaryTree();
int insert(const int value);
int preOrder() const;
int clearTree();
private:
BTNode *headPtr;
int insert(const int value, BTNode *leaf);
int clearTree(BTNode *leaf) const;
int preOrder(BTNode *leaf) const;
};
};
#endif
BinaryTree.cpp
#include"BinaryTree.h"
#include <iostream>
using namespace std;
SDI::BinaryTree::BinaryTree()
{
headPtr = nullptr;
}
SDI::BinaryTree::~BinaryTree()
{
//clearTree(headPtr);
}
int SDI::BinaryTree::insert(const int value, BTNode *leaf)
{
if (value < leaf->data)
{
if (leaf->left != nullptr)
{
insert(value, leaf->left);
}
else
{
leaf->left = new BTNode;
leaf->left->data = value;
leaf->left->left = nullptr;
leaf->left->right = nullptr;
}
}
else if (value >= leaf->data)
{
if (leaf->right != nullptr)
{
insert(value, leaf->right);
}
else
{
leaf->right = new BTNode;
leaf->right->data = value;
leaf->right->left = nullptr;
leaf->right->right = nullptr;
}
}
return 1;
}
Main.cpp
#include <string>
#include <iostream>
#include "Linkedlist.h"
#include "BinaryTree.h"
using namespace std;
int main( int argc, const char *argv[])
{
SDI::LinkedList myList;
//SDI::BinaryTree myTree;
int inp;
....
if (inp2 == 'a')
{
int num;
cout << "\nPlease enter a value to add: ";
cin >> num;
myTree.insert(num);
cout << "\n\t\tValue has been successfully saved\n\n\n";
}
I have tried looking up online, couldn't find any. It would be great help.
As I see you have two functions with name insert. And the first one
int insert(const int value);
was only declared but not defined.
Which part of the error message is unclear to you? It clearly states that you haven't implemented two of your methods:
int SDI::BinaryTree::insert(const int value) {
return 0; // the implementation probably needs some patching up
}
int SDI::BinaryTree::preOrder() const {
return 0; // as does this one
}