This question already has answers here:
C++ "fatal error LNK1120" unresolved static class members
(2 answers)
Closed 5 years ago.
I have a class EventType, which has the following header (relevant lines only):
#include<string>
#include<unordered_set>
#include<iostream>
class EventType
{
public:
static EventType* getByName(std::string name);
static EventType* getByID(std::string id);
static void setAllEventTypes(std::unordered_set<EventType*> events);
//...
private:
static std::unordered_set<EventType*> allEvents; //stores all events
std::string name;
//...
std::string akaID;
};
And the source:
EventType* EventType::getByName(std::string name) {
foreach(EventType * event, EventType::allEvents) {
if(event->name == name) {
return event;
}
}
std::cout << "Error: Event with name " << name << "could not be found.\n";
}
EventType* EventType::getByID(std::string id) {
foreach(EventType * event, EventType::allEvents) {
if(event->akaID == id) {
return event;
}
}
std::cout << "Error: Event with aka.ID " << id << "could not be found.\n";
}
void EventType::setAllEventTypes(std::unordered_set<EventType*> events) {
EventType::allEvents = events;
}
Now I'm getting the LNK2001-error:
eventtype.obj : error LNK2001: unresolved external symbol ""private: static class std::unordered_set<class EventType *,struct std::hash<class EventType *>,struct std::equal_to<class EventType *>,class std::allocator<class EventType *> > EventType::allEvents" (?allEvents#EventType##0V?$unordered_set#PEAVEventType##U?$hash#PEAVEventType###std##U?$equal_to#PEAVEventType###3#V?$allocator#PEAVEventType###3##std##A)".
I get this error even when I'm not using any of the static methods from outside my EventType-class. Why does this happen? Shouldn't EventType be able to link to itself?
You declared allEvents but did not define it, you need to do that in your source file:
std::unordered_set<EventType*> EventType::allEvents;
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 5 years ago.
#include <iostream>
#include <map>
#include <string>
#include <cstdlib>
using namespace std;
class Shape
{
public :
virtual void draw()=0;
virtual ~Shape(){}
};
class Circle : public Shape
{
string color;
int x;
int y;
int radius;
public:
Circle(string color){
color = color;
}
void setX(int x) {
x = x;
}
void setY(int y) {
y = y;
}
void setRadius(int radius) {
radius = radius;
}
void draw() {
cout << "color :" << color << x << y ;
}
};
class ShapeFactory {
public:
static map<string, Shape*> circlemap;
static Shape* getcircle(string color)
{
Shape *mcircle;
mcircle = circlemap.find(color)->second;
if(mcircle == nullptr) {
mcircle = new Circle(color);
circlemap[color] = mcircle;
// circlemap.insert(std::make_pair(color,mcircle));
}
return mcircle;
}
};
class Flyweightpattern
{
public:
static string getRandomColor(string colors[]) {
int m_rand = rand() % 5;
return colors[m_rand];
}
static int getRandomX() {
return (int)(rand() % 100);
}
static int getRandomY() {
return (int)(rand() % 100);
}
};
int main()
{
string colors[] = { "Red", "Green", "Blue", "White", "Black" };
for(int i=0; i < 20; ++i) {
Circle *circle = dynamic_cast<Circle *>(ShapeFactory::getcircle(Flyweightpattern::getRandomColor(colors)));
circle->setX(Flyweightpattern::getRandomX());
circle->setY(Flyweightpattern::getRandomY());
circle->setRadius(100);
circle->draw();
}
getchar();
return 0;
}
I am getting the linking error during run is below :
flyweight_pattern.obj : error LNK2001: unresolved external symbol
"public: static class std::map,class std::allocator >,class Circle
*,struct std::less,class std::allocator > >,class
std::allocator,class std::allocator > const ,class
Circle *> > > ShapeFactory::circlemap"
(?circlemap#ShapeFactory##2V?$map#V?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##PAVCircle##U?$less#V?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std###2#V?$allocator#U?$pair#$$CBV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##PAVCircle###std###2##std##A)
I have a map in the ShapeFactory class and tried to creating filling the map in the class itself but still not able to resolve the problem.
You didn't defined circlemap, it's a static member, so you should define it (and initialize) in global scope:
map<string, Shape*> ShapeFactory::circlemap = {};
Integral nonvolatile static members can be initialized in class.
Oh, and it is not recommended to do using namespace std; in global scope, that leads to side effects.
You can write something like
using std::map;
to target selected id (map in this case), and you can write using in namespace that contains usage.
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.
I am working on an assignment for implementing queue functionality using only 2 stacks. I have the idea and the code down completely as far as I know but I am getting this error. From what I've looked at it is because there are multiple declarations for 1 variable that occur in the various .cpp files. I do not understand this error very well. My only guess is that I have a function for stack called "empty" and I have a similar function in queue by the same name. Could someone please explain to me where I am going wrong? Thanks for any help!
My code
EDIT:
Exact error message:
SQueueMain.obj : error LNK2005: "public: __thiscall Stack::Stack(void)" (??0Stack##QAE#XZ) already defined in LStack.obj
SQueueMain.obj : error LNK2005: "public: __thiscall Stack::Stack(class Stack const &)" (??0Stack##QAE#ABV0##Z) already defined in LStack.obj
SQueueMain.obj : error LNK2005: "public: __thiscall Stack::~Stack(void)" (??1Stack##QAE#XZ) already defined in LStack.obj
SQueueMain.obj : error LNK2005: "public: class Stack const & __thiscall Stack::operator=(class Stack const &)" (??4Stack##QAEABV0#ABV0##Z) already defined in LStack.obj
SQueueMain.obj : error LNK2005: "public: bool __thiscall Stack::empty(void)const " (?empty#Stack##QBE_NXZ) already defined in LStack.obj
SQueueMain.obj : error LNK2005: "public: void __thiscall Stack::push(int const &)" (?push#Stack##QAEXABH#Z) already defined in LStack.obj
SQueueMain.obj : error LNK2005: "public: void __thiscall Stack::display(class std::basic_ostream<char,struct std::char_traits<char> > &)const " (?display#Stack##QBEXAAV?$basic_ostream#DU?$char_traits#D#std###std###Z) already defined in LStack.obj
SQueueMain.obj : error LNK2005: "public: int __thiscall Stack::top(void)const " (?top#Stack##QBEHXZ) already defined in LStack.obj
SQueueMain.obj : error LNK2005: "public: void __thiscall Stack::pop(void)" (?pop#Stack##QAEXXZ) already defined in LStack.obj
SQueueMain.obj : error LNK2005: "public: __thiscall queue::queue(void)" (??0queue##QAE#XZ) already defined in SQueue.obj
SQueueMain.obj : error LNK2005: "public: __thiscall queue::~queue(void)" (??1queue##QAE#XZ) already defined in SQueue.obj
SQueueMain.obj : error LNK2005: "public: void __thiscall queue::enqueue(int)" (?enqueue#queue##QAEXH#Z) already defined in SQueue.obj
SQueueMain.obj : error LNK2005: "public: void __thiscall queue::dequeue(void)" (?dequeue#queue##QAEXXZ) already defined in SQueue.obj
SQueueMain.obj : error LNK2005: "public: int __thiscall queue::front(void)" (?front#queue##QAEHXZ) already defined in SQueue.obj
SQueueMain.obj : error LNK2005: "public: bool __thiscall queue::empty(void)" (?empty#queue##QAE_NXZ) already defined in SQueue.obj
SQueueMain.obj : error LNK2005: "private: class Stack::Node * __thiscall queue::setFront(void)" (?setFront#queue##AAEPAVNode#Stack##XZ) already defined in SQueue.obj
SQueueMain.cpp
//SQueueMain.cpp starts here---------------------------------------
#include "SQueue.cpp"
int main()
{
queue queue1;
int switchNum, tempNum;
while (1)
{
cout << "What action would you like to perform? \n";
cout << "Please enter the number that corresponds with the correct action. \n";
cout << "0: Enqueue an item. \n";
cout << "1: Dequeue an item. \n";
cout << "2: Return the front value. \n";
cout << "3: Determine if the queue is empty. \n";
cout << "4: Exit the program. \n";
cin >> switchNum;
switch (switchNum)
{
case 0:
{
cout << "What is the value you wish to enqueue? \n";
cin >> tempNum;
queue1.enqueue(tempNum);
}
break;
case 1:
{
queue1.dequeue();
}
break;
case 2:
{
cout << queue1.front();
}
break;
case 3:
{
cout << queue1.empty();
}
break;
case 4:
{
return 0;
}
}
}
}
//SQueueMain.cpp ends here-----------------------------------------
SQueue.cpp
//SQueue.cpp starts here---------------------------------------------------
#include "SQueue.h"
#include <new>
using namespace std;
queue::queue() : myFront(0) {}
queue::~queue()
{
queue::s1.~Stack();
queue::s2.~Stack();
}
void queue::enqueue(int num)
{
queue::s1.push(num);
}
void queue::dequeue()
{
queue::setFront();
queue::s2.pop();
}
queueElement queue::front()
{
queue::setFront();
if (!empty())
return (myFront->data);
else
{
cerr << "*** Queue is empty -- returing garbage ***\n";
return *(new queueElement);
}
}
bool queue::empty()
{
queue::setFront();
return (myFront == 0);
}
Stack::NodePointer queue::setFront()
{
int temp;
if (queue::s2.empty() == true)
{
while (queue::s1.empty() != true)
{
temp = queue::s1.myTop->data;
queue::s1.pop();
queue::s2.push(temp);
}
}
myFront = queue::s2.myTop;
return myFront;
}
//SQueue.cpp ends here---------------------------------------------------
SQueue.h
//SQueue.h starts here-------------------------------------------------------------
#include <iostream>
#include "LSTack.cpp"
using namespace std;
#ifndef SQUEUE
#define SQUEUE
typedef int queueElement;
class queue
{
public:
Stack s1, s2;
queue();
~queue();
void enqueue(int);
void dequeue();
queueElement front();
bool empty();
Stack::NodePointer myFront; //typedef from LStack.h
private:
Stack::NodePointer setFront();
};
#endif
//SQueue.h ends here--------------------------------------------------------------
LStack.cpp
//--- LStack.cpp -------------------------------------------------
#include <new>
using namespace std;
#include "LStack.h"
//--- Definition of Stack constructor
Stack::Stack(): myTop(0){}
//--- Definition of Stack copy constructor
Stack::Stack(const Stack & original)
{
myTop = 0;
if (!original.empty())
{
// Copy first node
myTop = new Stack::Node(original.top());
// Set pointers to run through the stack's linked lists
Stack::NodePointer lastPtr = myTop,
origPtr = original.myTop->next;
while (origPtr != 0)
{
lastPtr->next = new Stack::Node(origPtr->data);
lastPtr = lastPtr->next;
origPtr = origPtr->next;
}
}
}
//--- Definition of Stack destructor
Stack::~Stack()
{
// Set pointers to run through the stack
Stack::NodePointer currPtr = myTop, // node to be deallocated
nextPtr; // its successor
while (currPtr != 0)
{
nextPtr = currPtr->next;
delete currPtr;
currPtr = nextPtr;
}
}
//--- Definition of assignment operator
const Stack & Stack::operator=(const Stack & rightHandSide)
{
myTop = 0;
if (rightHandSide.empty()) return *this;
if (this != & rightHandSide) // check that not st = st
{
this->~Stack(); // destroy current linked list
// Copy first node
myTop = new Stack::Node(rightHandSide.top());
// Set pointers to run through the stacks' linked lists
Stack::NodePointer lastPtr = myTop,
rhsPtr = rightHandSide.myTop->next;
while (rhsPtr != 0)
{
lastPtr->next = new Stack::Node(rhsPtr->data);
lastPtr = lastPtr->next;
rhsPtr = rhsPtr->next;
}
}
return *this;
}
//--- Definition of empty()
bool Stack::empty() const
{
return (myTop == 0);
}
//--- Definition of push()
void Stack::push(const StackElement & value)
{
myTop = new Stack::Node(value, myTop);
}
//--- Definition of display()
void Stack::display(ostream & out) const
{
Stack::NodePointer ptr;
for (ptr = myTop; ptr != 0; ptr = ptr->next)
out << ptr->data << endl;
}
//--- Definition of top()
StackElement Stack::top() const
{
if (!empty())
return (myTop->data);
else
{
cerr << "*** Stack is empty "
" -- returning garbage ***\n";
return *(new StackElement); // "Garbage" value
}
}
//--- Definition of pop()
void Stack::pop()
{
if (!empty())
{
Stack::NodePointer ptr = myTop;
myTop = myTop->next;
delete ptr;
}
else
cerr << "*** Stack is empty -- can't remove a value ***\n";
}
LStack.h
#include <iostream>
using namespace std;
#ifndef LSTACK
#define LSTACK
typedef int StackElement;
class Stack
{
friend class queue;
public:
Stack();
Stack(const Stack & original);
~Stack();
const Stack & operator = (const Stack & rightHandSide);
bool empty() const;
void push(const StackElement & value);
void display(ostream & out) const;
StackElement top() const;
void pop();
/***** Function Members *****/
// Prototypes same as in preceding section
private:
/*** Node class ***/
class Node
{
public:
StackElement data;
Node * next;
//--- Node constructor
Node(StackElement value, Node * link = 0)
/*------------------------------------------------------
Precondition: value is received
Postcondition: A Node has been constructed with value
in its data part and itb next part set to link
(default 0).
------------------------------------------------------*/
{ data = value; next = link; }
};
typedef Node * NodePointer;
/***** Data Members *****/
NodePointer myTop; // pointer to top of stack
}; // end of class declaration
#endif
#include "SQueue.cpp" // KILL THIS or exclude SQueue.cpp in compilation process
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.