"Unresolved External Symbol" errors when creating object in C++ - c++

I'm a pretty seasoned programmer but I'm just now diving into C++ and it's... well... more difficult than PHP and Python. I keep having unresolved external errors when trying to create an object from some classes. It's broken up into multiple headers and files but here is a basic idea from one of my classes:
die.h:
#ifndef DIE_H
#define DIE_H
using namespace std;
class Die {
public:
int throwDie();
Die();
};
#endif
die.cpp
#include <iostream>
#include <cstdlib>
#include "Die.h"
using namespace std;
int Die::throwDie()
{
return 0;
}
sixsidedie.h
#ifndef SIXSIDEDIE_H
#define SIXSIDEDIE_H
#include "Die.h"
using namespace std;
class SixSideDie : public Die
{
public:
SixSideDie();
int throwDie();
private:
int randNumber;
};
#endif
sixsidedie.cpp
#include <iostream>
#include <cstdlib>
#include <time.h>
#include "Die.h"
#include "SixSideDie.h"
using namespace std;
const int SIX_SIDE = 6;
int SixSideDie::throwDie()
{
srand((unsigned int)time(0));
SixSideDie::randNumber = rand() % SIX_SIDE + 1;
return SixSideDie::randNumber;
}
main.cpp
#include <iostream>
#include <cstdlib>
#include "Die.h"
#include "SixSideDie.h"
#include "TenSideDie.h"
#include "TwentySideDie.h"
using namespace std;
int main()
{
Die* myDice[3];
myDice[0] = new SixSideDie();
myDice[1] = new TenSideDie();
myDice[2] = new TwentySideDie();
myDice[0]->throwDie();
myDice[1]->throwDie();
myDice[2]->throwDie();
system("pause");
return 0;
}
It keeps telling me that each object I create directly above is an unresolved external symbol and I just don't know why. Any thoughts!?

You declared a constructor for Die but never defined it.
Also, you almost certainly want throwDie to be virtual if you intend to override its behavior in derived classes, and you should never use using namespace std; in a header file (and many people, including me, would argue that you shouldn't use it at file-scope at all).

You didn't define your constructor in your cpp files.

Its good practice to define the constructors of the classes. Check this out:
#ifndef DIE_H
#define DIE_H
using namespace std;
class Die {
public:
int throwDie();
Die() { }; // can you spot the difference here?
};
#endif

Related

Undefined Reference when compiling C++

My code is similar to this one, but the problem is exactly the same: I'm getting an "undefined reference to `Test1::v" in Test1.cpp and in Test2.cpp when compilling the program in VSCode. What am I doing wrong? I'm a bit new on c++ so I just downloaded an extension that made me a project in c++ automatically. When I run the program using Ctrl + Shift + B it gives me this error, but when I do it with the Code Runner extension it doesn't detect the .cpp files.
// Test1.h
#include <iostream>
#include <vector>
using namespace std;
#ifndef TEST1_H
#define TEST1_H
class Test1{
public:
Test1();
static vector<Test1> v;
int a;
};
#endif
//Test1.cpp
#include "Test1.h"
Test1::Test1(){
a = 2;
v.push_back(*this);
}
//Test2.h
#include <iostream>
#include <vector>
using namespace std;
#ifndef TEST2_H
#define TEST2_H
class Test2{
public:
Test2();
double var;
};
#endif
//Test2.cpp
#include "Test2.h"
#include "Test1.h"
Test2::Test2(){
var = 5;
Test1::v[0].a += var;
}
//main.cpp
#include <iostream>
#include "Test1.h"
#include "Test2.h"
using namespace std;
int main(int argc, char *argv[])
{
cout << "Hello world!" << endl;
}
You have declared the static vector in the header file, but you need to define it in a cpp file. Add:
vector<Test1> Test1::v;
to your test1.cpp file. You can learn more about definition vs declaration here.
Also make sure you read this: Why is "using namespace std;" considered bad practice?
You could prepend the class name to call the variable directly since it's static. So, you could do something like:
Test1::Test1(){
// v.push__back(*this); // previous
Test1::v.push_back(*this); // now
}
in Test1.cpp. You'll then get a reference tooltip on your VS Code:
static std::vector<Test1> Test1::v
Which proves it's done.

Redefinition error: Different .h files using the same class name

I've created 2 header files. ListA.h and ListN.h
They both make their own use their own unique class List. When I compile my program (even though they have no way of knowing the other exists, it says the following error)
Im pretty sure it shouldnt be a redefinition, but it obviously is. Any help is appreciated.
ListA.h
#ifndef __LISTA_H_
#define __LISTA_H_
#include <iostream>
using namespace std;
class List{
public:
List(int = 0);
List(const List&);
~List();
};
#endif
ListN.h
#ifndef __LISTN_H_
#define __LISTN_H_
#include <iostream>
using namespace std;
class List{
public:
List(int = 10);
List(const List&);
~List();
};
#endif
ListA.cpp
#include "ListA.h"
using namespace std;
List::List(int mySize)
{
//...
}
ListN.cpp
#include "ListN.h"
#include <iostream>
using namespace std;
List::List(int size)
{
//...
}
Main
#include <iostream>
#include "ListN.h"
using namespace std;
int main()
{
List myList;
return 0;
}
Both cpp files are being compiled by the compiler. Thus, when the linker goes to link the files together, it gets confused, since there are multiple List classes.
To fix this, you could use namespaces, or you cold not expose at least one of the List classes.
Alternatively, if the idea was to be able to include ListN.h vs ListA.h for configuration purposes, this is the wrong way to do so. Either you should have a #define parameter for the header, or you should find some other way, such as through #ifdef. For example (I'm not 100% sure this would compile, but you get the idea):
List.h
#ifndef __LIST_H_
#define __LIST_H_
#ifndef LIST_PARAM
#define LIST_PARAM 0
#endif
#include <iostream>
using namespace std;
class List{
public:
List(int = LIST_PARAM);
List(const List&);
~List();
};
#endif
main.cpp
#include <iostream>
#define LIST_PARAM 10
#include "List.h"
using namespace std;
int main()
{
List myList;
return 0;
}
I personally don't like this method; it is much better to just pass the value in to the constructor:
int main()
{
List myList{ 10 };
return 0;
}
When linker trying to link find the definition / symbol for List, it does found in two different obj file and hence linker givers error. In visual studio error number : LNK2005
To solve this error, either:
To fix, add /FORCE:MULTIPLE to the linker command line options
Add the classes in two different namespaces which will avoid this error.
ListN.h
#ifndef __LIST_H_
#define __LIST_H_
#include <iostream>
using namespace std;
namespace ListN
{
class List{
public:
List(int = 10);
List(const List&);
};
}
#endif
ListN.cpp
#include "ListN.h"
#include <iostream>
using namespace std;
namespace ListN
{
List::List(int size)
{
//...
}
}
Main.cpp
#include <iostream>
#include "ListN.h"
int main()
{
ListN::List myList;
return 0;
}

Multiple Class Files C++

I am unsure about the use of separate files for classes. How do I make functions inside the classes? Where do I put it?
QuizMain.cpp:
#include "QuizMain.h"
#include <iostream>
#include <string>
using namespace std;
QuizMain::QuizMain()
{
// Hia stackoverflow
}
QuizMain.h file:
#ifndef QUIZMAIN_H
#define QUIZMAIN_H
#include <string>
using namespace std;
class QuizMain
{
public:
QuizMain();
private:
};
#endif // QUIZMAIN_H
Main file:
#include <iostream>
#include <string>
#include "QuizMain.h"
using namespace std;
int main()
{
QuizMain qm;
return 0;
}
How would I make a class and call it correctly?
This is an example:
QuizMain.cpp file:
#include "QuizMain.h"
#include <iostream>
#include <string>
using namespace std;
QuizMain::QuizMain()
{
// Hia stackoverflow
}
void QuizMain::my_new_function(std::string my_name){
std::cout << "Hi " + my_name +"!" << std::endl;
}
QuizMain.h file:
#ifndef QUIZMAIN_H
#define QUIZMAIN_H
#include <string>
class QuizMain
{
public:
QuizMain();
void my_new_function(std::string my_name);
private:
};
#endif // QUIZMAIN_H
Main file:
#include <iostream>
#include <string>
#include "QuizMain.h"
using namespace std;
int main()
{
QuizMain qm;
qm.my_new_function("foo");
return 0;
}
Anyway, there is no point from asking such a question here. You should probably find a good book/resource and learn how to write and use functions.
Normally you have a header file and cpp file. The header file is where you declare your functions and member variables. The cpp file is where you implement your functions.
quizmain.h
// QuizMain.h file
#ifndef QUIZMAIN_H
#define QUIZMAIN_H
#include <string>
class QuizMain
{
public:
QuizMain(int quizScore);
// declare public functions here
private:
int quizScore; // declare private member variables here.
};
#endif // QUIZMAIN_H
cpp file
// QuizMain.cpp file
#include "QuizMain.h"
#include <iostream>
#include <string>
using namespace std;
QuizMain::QuizMain(int quizScore)
{
this.quizScore = quizScore; // init a quiz score
}
main
Call and create a class object like this
QuizMain quiz(95);
It is easy to do.
If you use the IDE project, the IDE set you to use the any file. Like code::block IDE But if you do not use the IDE project it is a little different to use.
You should write the .h file and then,after all writing you should put .cpp file.
Also you can use interface class that works by poiter.
/// .h file and declaration
#ifndef ONE.H
#define ONE.H
class one {
public:
one();
~one();
};
#include "one.cpp"
#endif ONE.H
then:
/// .cpp file and implementation
one::one(){
std::cout<<"constructor one"<<std::endl;
}
one::~one(){
std::cout<<"destructor one"<<std::endl;
}
then :
#include <iostream>
#include "one.h"
int main()
{
one o;
}
output:
constructor one
destructor one
Process returned 0 (0x0) execution time : 0.010 s
Press ENTER to continue.

using vector in c++ class definition

I have a mysterious problem. I keep getting a ‘vector’ does not name a type error when trying to define a variable in the tour class. The library seems installed correctly, since I can define vectors in main. According to many posts here I have checked for the right vector includes and std namespace. Still I get the error.
main.cpp
#include <vector>
#include <iostream>
#include <cstring>
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <cmath>
#include <cstdlib>
#include "tour.h"
using namespace std;
int main () {
//vector declaration works here
return 0;
}
tour.h
#ifndef TOUR_H_
#define TOUR_H_
#include<vector>
using namespace std;
class tour
{
public:
//variables
int teamID;
vector<int> tourseq; //this is where the error occurs
//functions
tour(int);
};
#endif /* TOUR_H_ */
tour.cpp
#include<vector>
#include "tour.h"
using namespace std;
tour::tour(int id){
teamID = id;
}
What could be wrong here?
Instead of writing using namespace std; and vector<int> tourseq; consider writing std::vector<int> tourseq;.
You probably shouldn't put using namespace std; in your code anyway.

Trouble with using class types within other classes

I have absolutely no idea what's going on. I've been looking up explanations for the weirdness going on here but it seems my situation is in some ways unique. I imagined it was the order in which I include my header files in each of my files, but to no avail, I have not found a combination that seems to be the solution.
The exact error I seem to be getting is "log does not name a type" when declaring LogArray[maxLength].
One of my classes, class logmgmt:
class logmgmt
{
private:
static const int maxLength = 500;
log LogArray[maxLength];
int length;
public:
void fillLogs(int index, int iD, std::string date, double startTime, double endTime);
void displayThisLog(int index);
void setLength(int length);
};
Pre-processor directives within logmgmt.cpp:
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
#include "log.h"
#include "Logmgmt.h"
And directives within main.cpp
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
#include "employee.h"
#include "log.h"
#include "employeemgmt.h"
#include "Logmgmt.h"
Remove using namespace std.
That is polluting the global namespace with lots of symbol names that can cause these conflicts.
In your example, the function std::log becomes log. So it can no longer name a global type.