Why am I getting undefined reference errors? [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 2 years ago.
I'm getting undefined reference errors. Can you please figure out why?
Errors:
undefined reference to Breuken::Breuken(int, int)
undefined reference to Breuken::som()
(I'm kinda new in c++ programming. I am currently working on a project now, using header files another source file and main source file.)
Here is my code:
Header File (Breuk.H):
using namespace std;
class Breuken{
Breuken(int, int);
void som();
void setTeller1(int);
void setNoemer1(int);
int getTeller1();
int getNoemer1();
private:
int teller1;
int noemer1;
};
Source File(Breuk.cpp):
#include <iostream>
#include "Breuk.h"
using namespace std;
Breuken::Breuken(int tel1, int noem1)
{
setTeller1(tel1);
setNoemer1(noem1);
}
int Breuken::getTeller1()
{
return teller1;
}
int Breuken::getNoemer1()
{
return noemer1;
}
void Breuken::setTeller1(int tel1)
{
teller1 = tel1;
}
void Breuken::setNoemer1(int noem1)
{
noemer1 = noem1;
}
int Breuken::som()
{
cout<<"Breuken zijn: "<<getTeller1()<<" / "<<getNoemer1()<<endl;
}
main file(main.cpp):
#include <iostream>
#include "Breuk.h"
using namespace std;
int main()
{
Breuken br(1, 2);
br.som();
return 1;
}

If I got this right, you want to use a class, but the only thing you did is declare some functions without actually defining them.
What you need to do is put everything in your header inside a class like that:
class Breuk {
private:
int teller1;
int noemer1;
public:
Breuk(int tel1, int noem1);
void som();
void setTeller1(int tel1);
void setNoemer1(int noem1);
int getTeller1();
int getNoemer1();
};
Also, don't forget to give the variables inside the function head a name!
The .cpp file you need to get the reference to the class right (the part before the ::). It has to be the same name as your class. Also, it's not so great to use setter inside the constructor. It's better to reference the member variables directly (the variables inside the class). In order to do that you need to use "this->variable". "this" will refer to the current class you're in. That means if you write this->teller1 it will refer to the variable teller1 inside the class.
#include <iostream>
#include "Breuk.hpp"
using namespace std;
Breuk::Breuk(int tel1, int noem1)
{
// setTeller1(tel1); theoreticly ok but its better to access
// setNoemer1(noem1); member variables (variables inside a class) directly
this->teller1 = tel1;
this->noemer1 = noem1;
}
int Breuk::getTeller1()
{
return teller1;
}
int Breuk::getNoemer1()
{
return noemer1;
}
void Breuk::setTeller1(int tel1)
{
teller1 = tel1;
}
void Breuk::setNoemer1(int noem1)
{
noemer1 = noem1;
}
void Breuk::som()
{
cout << "Breuken zijn: " << getTeller1() << " / " << getNoemer1() << endl;
}
in the main.cpp you only made a small mistake with the return 0 and the name of the class.
#include "Breuk.hpp"
using namespace std;
int main()
{
Breuk br(1, 2); // make sure u use the same name your class has
br.som();
return 0; // return 0 at the end: it means that the program executed correctly
// return 1 would mean that an error occured
}
Also, I suggest using .hpp files instead of .h files. There is no real difference but it's more common to use .hpp for c++.

Related

Function was not declared in this scope, even though header file is present

I'm trying to run some test code to learn c++, but I am getting an error telling me the reverseDigits function was not declared in the main.cpp scope:
error: 'reverseDigits' was not declared in this scope.
But the #include "Solutions.h" header was included in main.cpp, so I thought that it would be in scope.
I have checkout other questions, but the answers all relate to problems with circular header file inclusion, which I don't think is the problem here.
Do you know why I am seeing that error?
Solution.h
#ifndef SOLUTION_H
#define SOLUTION_H
class Solution {
public:
Solution();
~Solution();
int reverseDigits(int x);
};
#endif // SOLUTION_H
Solution.cpp
#include "Solution.h"
#include <string>
Solution::Solution()
{
}
Solution::~Solution()
{
}
int Solution::reverseDigits(int x) {
std::string num_string = std::to_string(x);
std::string reversed_num_string {};
for (int i = num_string.length() - 1; i > 0; i--) {
reversed_num_string.push_back(num_string[i]);
}
return stoi(reversed_num_string);
}
main.cpp
#include <iostream>
#include "Solution.h"
int main()
{
int x {123};
int result = reverseDigits(x);
std::cout << result << std::endl;
return 0;
}
You declared reverseDigits as a member function of the Solution class, then defined it without qualifying it as a member of Solution (Edit: You've since changed it to match declaration and definition, but at point of use, you're trying to use an unqualified function, not a member of a Solution object). The declaration in the .h file is visible, but the definition in the .cpp is unrelated, and not visible to main.cpp.
Declare the function outside the class (since it's clearly unrelated to the class), and it should work, changing to:
class Solution {
public:
Solution();
~Solution();
};
int reverseDigits(int x); // NOT inside the class definition
I'll note: I have no idea why you have a Solution class at all. Defining reverseDigits doesn't require it, so I'm not seeing the point. If this is part of some automated evaluation framework, you'll have to give more details
Along with ShadowRanger's valid suggestion, I'll highlight upon how you could have used the data as part of your Solution class and applied the function on it.
Refactoring your class to
class Solution {
public:
Solution(int data);
~Solution();
int reverseDigits();
private:
int m_data;
};
Solution::Solution(int data)
{
m_data = data;
}
Solution::~Solution()
{
}
Even though you could have used std::reverse, fixing the error on the i>=0 is needed to have your own reverse function
int Solution::reverseDigits() {
std::string num_string = std::to_string(m_data);
std::string reversed_num_string {};
for (int i = num_string.length() - 1; i >= 0; i--) {
reversed_num_string.push_back(num_string[i]);
}
return stoi(reversed_num_string);
}
Now call it from your main() as
int main() {
int x = 123;
Solution sol(x);
std::cout << sol.reverseDigits() << std::endl;
return 0;
}

unable to access static methods (C++)

Here's the code:
#include <iostream>
using namespace std;
class Zaix
{
private:
static int mor;
public:
static int beri;
static void setmor(int lip)
{
Zaix::mor=lip;
}
static int getmor(void)
{
return mor;
}
};
int Zaix::beri=3;
int main()
{
cout<<Zaix::beri<<endl;
Zaix::beri++;
cout<<Zaix::beri<<endl;
Zaix::setmor(6);
return 0;
}
Now, line 4 of main() function Zaix::setmor(6); somehow invalidates line 11 of the code presented Zaix::mor=lip;. With this line commented out, the whole thing compiles OK, with it present, compiler gives this error:
undefined reference to Zaix::mor"
Any idea why that is?
Define the variable outside class as well.
int Zaix::mor;
For assignment:
int Zaix::mor = 4;
In C++ we need to define all the static member variable of a class outside of it else we get a linking error. You just need to do like below:-
int Zaix::mor;// Just add this line below int Zaix::beri = 3;

Returning int from object function call (basic) [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've just started looking at C++ and I am having some problems I can't wrap my head around.
I am trying to do something as simple as creating a class with a function, call that function and read the returned value.
However I am stuck at what I guess is a minor issue.
I know that it is the call from main to Die -> randomNumber that causes this error but I dont know why. I even tried to return the number 12 from the function without any luck.
Error:
I have 3 files (main.cpp, Die.h and Die.cpp)
main.cpp
#include <string>
#include "Die.h"
using namespace std;
int main() {
Die * test = new Die();
cout << std::to_string(test->randomNumber(4)) << endl;
return 0;
}
Die.h
#pragma once
#include <iostream>
#include <ctime>
#include <cstdlib>
class Die
{
public:
Die();
~Die();
void init();
void rollDie(int&, int&);
void rollDie(int*, int*);
int randomNumber(int);
};
Die.cpp
#include "Die.h"
Die::Die()
{
}
Die::~Die()
{
}
void Die::init() {
}
int randomNumber(int max) {
srand(static_cast<unsigned int>(time(0)));
return (rand() % max + 1);
}
void rollDie(int& die1, int& die2) {
}
void rollDie(int* die1, int* die2) {
}
You're missing the class specifier on the declaration of randomNumber and rollDie.
int Die::randomNumber(int max)
Without those you're just declaring a global function named randomNumber, but the linker is attempting to choose Die::randomNumber which it cannot find.
In Die.cpp, you need to prefix member functions with the class name:
int Die::randomNumber(int max) { ... }
void Die::rollDie(int& die1, int& die2) { ... }
void Die::rollDie(int* die1, int* die2) { ... }
Without that, the compiler thinks you're just defining global functions. It has no reason to suspect they should be members to a class if you don't tell it.

c++: undefined reference to vtable when using header but not without [duplicate]

This question already has answers here:
C++ Undefined Reference to vtable and inheritance
(3 answers)
Closed 9 years ago.
I have a interface and I want to create a header with the functions from the interface and a .cpp implementing the functions in this header. But when try this I always get the problem undefined reference to 'vtable for Test' in the Testt.h file.
I'm working on a rather big project in eclipse so I reduced my problem to a few small classes.
ITestAdapter.h:
#ifndef ITESTADAPTER_H_
#define ITESTADAPTER_H_
class TestAdapter {
public:
virtual int test() = 0;
};
#endif /* ITESTADAPTER_H_ */
Testt.h:
#ifndef TESTT_H_
#define TESTT_H_
#include "ITestAdapter.h"
class Test: public TestAdapter{
public:
virtual int test();
};
#endif /* TESTT_H_ */
Testt.cpp:
#include "Testt.h"
int test() {
return 0;
}
Test_main.cpp:
#include <iostream>
#include "Testt.h"
using namespace std;
int main() {
Test t;
int i = t.test();
cout << i << endl;
return 0;
}
If I don't use the Testt.h at all and implement the interface within Testt.cpp and include Testt.cpp (what I want to avoid) within the file with my main-method then it works fine.
Testt.cpp (modified):
#include "ITestAdapter.h"
class Test: public TestAdapter {
public:
int test() {
return 0;
}
};
So I don't understand why it doesn't work if I use a header (which I think would be the nicer solution).
I hope I could explain clearly what my problem is. If not please ask.
You are defining a non-member function int test() in Testt.cpp. You need to define int Test::test():
int Test::test()
{// ^^^^
return 0;
}
Undefined reference to X means that the linker can't find the definition of X that has been declared.
You declared Test to have a member function int test() but this
int test() {
return 0;
}
defines a free function.
You need
int Test::test() {
return 0;
}
undefined reference to 'vtable for Test'" to test is a bit confusing. It usualy means that you forgot to define the first virtual function of your class.

Unidentified reference to constructor (c++) [duplicate]

This question already has answers here:
Undefined reference C++
(2 answers)
Closed 9 years ago.
I have done some research and googling on this error for days, as far as I know this is a common problem for many in c++, I still haven't found a clear answer to this error. I've read that linking the files can fix this, but I could find any example codes to do this. I'am so close to finishing this code, all I need to do to call the constructor from the main file (or just make a simple object), but I keep getting this "unidentified reference to NamedStorm::NamedStorm()" error, please help.
MAIN.CPP
#include <iostream>
#include <string>
#include "NamedStorm.h"
using namespace std;
NamedStorm storm[2];
int main(){
// NamedStorm Chris("Chris", 70.0, "T.S.", 990.0);
// storm[0] = Chris;
return 0;
}
NamedStorm.cpp
// CPP => Function definition
#include <string>
#include "NamedStorm.h"
using namespace std;
// Defining static variables
int NamedStorm::stormCount = 0;
// Constructor definition
NamedStorm::NamedStorm(std::string sName, double wSpeed, std::string sCat, double sPress){
stormName = sName;
windSpeed = wSpeed;
stormCategory = sCat;
stormPressure = sPress;
stormCount++;
}
NamedStorm::NamedStorm(std::string sName){
stormName = sName;
stormCount++;
}
NamedStorm::NamedStorm(){
stormName = sName;
stormCount++;
}
// Destructor definition
//NamedStorm::~NamedStorm(){}
// Get (Accessor) function definition
int NamedStorm::getStormCount(){
return stormCount;
}
double NamedStorm::getStormPressure(){
return stormPressure;
}
string NamedStorm::getStormCategory(){
return stormCategory;
}
string NamedStorm::getName(){
return stormName;
}
// Set (Mutator) function definition
void NamedStorm::displayOutput(){}
void NamedStorm::sortByNames(){}
void NamedStorm::getAverageStormPressure(){}
void NamedStorm::getAverageWindSpeed(){}
void NamedStorm::getWindSpeed(){}
NamedStorm.h
#ifndef NAMEDSTORM_H_INCLUDED
#define NAMEDSTORM_H_INCLUDED
// NEVER use using namespce in header, use std instead.
class NamedStorm{
private:
std::string stormName;
std::string stormCategory;
double maxWindSpeed;
double stormPressure;
static int stormCount;
public:
// Constructor
NamedStorm(std::string, double, std::string, double);
NamedStorm(std::string);
NamedStorm();
// Destructor
//~NamedStorm();
// Get functions
int getStormCount();
double getStormPressure();
double getWindSpeed();
std::string getStormCategory();
std::string getName();
// Set functions
static void displayOutput();
static void sortByNames();
static void sortByWindSpeed();
static void getAverageWindSpeed();
static void getAverageStormPressure();
};
#endif // NAMEDSTORM_H_INCLUDED
Why is the definition of the default constructor the same as of the NamedStorm::NamedStorm(std::string) ? I would start from correcting that.