Unresolved external symbol in a class function [duplicate] - c++

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 6 years ago.
can anyone tell me what is the problem with the following code:
#include <iostream>
#include <string>
using namespace std;
class Exception
{
string err;
public:
Exception (string _err) : err(_err) {}
const string& Err ();
};
int main()
{
Exception exc("error");
cout << exc.Err() << endl;
}
I get unresolved external symbol error on call to the function Exception::Err.
EDIT: Now I see what's the problem, I'm sorry for asking such a stupid question. But I really did spent half an hour looking at the code trying to figure out what's wrong.
Might aswell delete the question to save me from bad reputation. :D And you guys from a bad read. ;)

The error means you didn't define one of your member functions. In this case, you don't have a function body for const string& Err ();

Related

Linking problems with g++ and clang [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
How does the compilation/linking process work?
(5 answers)
Closed 1 year ago.
I am sure this question has be asked and answered million times but I am really clueless. As a newbie to C++ I have created three textbook files.
Num.h
#ifndef LIB_DATE_H
#define LIB_DATE_H
class Num
{
private:
int num;
public:
Num(int n);
int getNum();
};
#endif
Num.cpp
#include "Num.h"
Num::Num(int n) : num(n) {}
int Num::getNum()
{
return num;
}
main.cpp
#include <iostream>
#include "Num.h"
using namespace std;
int main()
{
Num n(35);
cout << n.getNum() << endl;
return 0;
}
Both g++ and clang complain about undefined reference to Num::Num(int) and Num::getNum().
When joining the three files into a single one, no errors are reported by g++; clang is still complaining.
I have managed to google out that this is a problem with linking but I was not able to find any actionable advice to fix it...

C++ Linker Error. Undefined reference to Class::Function() [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
How does the compilation/linking process work?
(5 answers)
Closed 2 years ago.
I am new to C++ and I am creating a very simple program using classes. I am using Visual Studio Code to write my code and MinGW compiler to build and run it.
I have written the following code as an example to show my problem.
Main.cpp
#include "class.h"
using namespace std;
int main(void) {
myClass myObject;
myObject.sum(10, 20);
return 0;
}
class.h
#pragma once
class myClass{
public:
void sum(int a, int b);
};
class.cpp
#include <iostream>
#include "class.h"
using namespace std;
void myClass::sum(int a, int b) {
cout << "The sum = " << a + b << endl;
}
The compiler is giving me this error when I try to build it.
undefined reference to `myClass::sum(int, int)'
collect2.exe: error: ld returned 1 exit status
BTW I know there are a lot of similar questions on the forum and I am sorry I am posting it again but all those solutions didn't seem to work for me as my program here is pretty simple. Any help will be very appreciated. Thanks!

VS is giving me another solution to my constructor but I want to know why mine wont work [duplicate]

This question already has answers here:
What is the difference between a definition and a declaration?
(27 answers)
Closed 3 years ago.
My constructor has a green line underneath it saying "function definition not found".
Visual Studio has given me a fix, but I want to know why mine doesn't work.
#pragma once
#include "class_dayType.h"
#include <iostream>
#include <string>
using namespace std;
int main() {
dayType day;
string d;
cout << "Enter day of week: ";
cin >> d;
day.set_day(d);
}
#include <iostream>
#include<string>
using namespace std;
class dayType {
public:
string day;
dayType(); //constructor with green line
void set_day(string day_of_week) {
string day = day_of_week;
}
};
Visual Studio created this in another file and it worked. What is the difference between this and my constructor?
dayType::dayType()
{
}
Errors:
LNK2019 unresolved external symbol "public: __thiscall dayType::dayType(void)" (??0dayType##QAE#XZ) referenced in function _main Day_of_Week
LNK1120 1 unresolved externals Day_of_Week
dayType();
This is not a definition, it's just a declaration. It indicates that a constructor (or any function) will be present somewhere in the code later on.
You would need
dayType()
{
}
Read more here and here.

Linker error LNK2019 on including SDL.h [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 6 years ago.
I'm trying to get my program to allow me to use C++'s input and output streams for debugging purposes but it won't work?
My code:
#include <SDL.h>
#include <iostream>
using namespace std;
int main()
{
cout << "I work!";
return 0;
}
That is all I have and it won't work. I plan to use SDL to make a small checkers game but I'd like to use <iostream> to debug.
Here's my errors:
error LNK2019: unresolved external symbol _SDL_main referenced in function _main_utf8
fatal error LNK1120: 1 unresolved externals
This is covered by FAQ:
Make sure that you are declaring main() as:
int main(int argc, char *argv[])
Since SDL_main is not special name known to C++ compiler, it mangles it by general rules. SDL have forward declaration with correct linking flags (at least extern "C") only for int SDL_main(int, char**).

Visual Studio takes two unresolved externals with the linkers LNK2019 [duplicate]

This question already has answers here:
unresolved external symbol __imp__fprintf and __imp____iob_func, SDL2
(19 answers)
Closed 7 years ago.
My code looks like this
#include <iostream>
#include <SDL2\SDL.h>
int main(int argc, char* args[])
{
char tep;
std::cin >> tep;
return 0;
}
Visual Studio now displays the following error:
LNK1120 2 unresolved externals
LNK2019 reference to unresolved external symbol "_imp_ printf" function in "_ShowError".
LNK2019 reference to unresolved external symbol "_imp_ _iob_func" in function "_ShowError".
I have really already searched many articles about this issue, but either that what it says is true not just my problem, or the solutions are not working.
And if it helps, I have been working on the following video:https://www.youtube.com/watch?v=uzwAYyK9ZBY&feature=iv&src_vid=TC0kHYRWX1Y&annotation_id=annotation_1897517141
Thanks in advance
Try put #undef main before your entry point. I had this exact problem. Try it, and let me know how it goes.
SDL for some reason defines "#define main SDL_main"
So try something like this:
#undef main
int main(int argc, char *args[])