Undefined reference to function of class [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 1 year ago.
I'm dealing with oop and cannot understand what is wrong. When i try to compile code i get next messsage: undefined reference to `N::my_class::do_something()'. Code is from microsoft: https://learn.microsoft.com/en-us/cpp/cpp/header-files-cpp?view=msvc-160
my_class.h file:
namespace N
{
class my_class
{
public:
void do_something();
};
}
my_class.cpp file:
#include "my_class.h" // header in local directory
#include <iostream> // header in standard library
using namespace N;
using namespace std;
void my_class::do_something()
{
cout << "Doing something!" << endl;
}
my_program.cpp file:
#include "my_class.h"
using namespace N;
int main()
{
my_class mc;
mc.do_something();
return 0;
}

Your code works for me on g++ 8.4.0.
using namespace N; might not enough to declare member function in N for your compiler.
Try to specify the namespace either inline or using a namespace scope in your cpp file.
EDIT: most likely problem. my_class.cpp is not compiled or linked. Check your compile parameters.

Related

"Undefined symbols for architecture arm64" - What does this mean? [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 11 months ago.
I'm unable to get my code to run, and the internet doesn't seem to know why. I'm not sure what I need to let you know, but I am using CLion if that helps.
This is my plant.h file:
#ifndef COURSEWORK_PLANT_H
#define COURSEWORK_PLANT_H
using namespace std;
class Plant {
public:
void addGrowth();
int getSize();
string getName();
Plant(string x, int y);
private:
string plantName;
int plantSize;
};
#endif //COURSEWORK_PLANT_H
This is my plant.cpp file:
#include <iostream>
#include "plant.h"
using namespace std;
void Plant::addGrowth(int x) {
plantSize += x;
cout << "You have added " << x << " leaves to your plant. Well done!";
}
int Plant::getSize() {
return Plant::plantSize;
}
string Plant::getName() {
return Plant::plantName;
}
This is my main.cpp file:
#include <iostream>
#include "plant.h"
using namespace std;
int main() {
Plant myPlant("Bugg", 2);
return 0;
}
This is my CMakeLists.txt file:
cmake_minimum_required(VERSION 3.21)
project(Coursework)
set(CMAKE_CXX_STANDARD 14)
add_executable(Coursework main.cpp plant.h plant.cpp)
Thank you in advance for any help!
Undefined symbol means that a symbol is declared but not defined.
For example in the class definition you have the following member function without parameters
void addGrowth();
But then you defined a function with the same name but now with one parameter
void Plant::addGrowth(int x) {
plantSize += x;
cout << "You have added " << x << " leaves to your plant. Well done!";
}
So the function declared in the class definition Plant is still undefined.
Also there is no definition of the constructor
Plant(string x, int y);
in the provided by you code.
And if you are using the name string from the standard library in the header plant.h then you need to include the header <string>
#include <string>

c++ 'undefined reference to' error

I'm having one of those "undefined reference to " errors when compiling a c++ program. I know this is common pitfall, but so far was unable to figure out what I'm doing wrong.
Here's the relevant code. Ex1Two_Sum.h:
#ifndef EX1TWO_SUM_H
#define EX1TWO_SUM_H
#include <vector>
using namespace std;
namespace ddc {
class Ex1Two_Sum
{
public:
void f();
protected:
private:
};
}
#endif
Ex1Two_Sum.cpp:
#include <vector>
#include <cstddef>
#include <iostream>
using namespace std;
namespace ddc {
class Ex1Two_Sum {
public:
void f(){
cout << "works" << endl;
}
};
}
And finally, main.cpp:
#include <iostream>
#include "Ex1Two_Sum.h"
using namespace std;
using namespace ddc;
int main()
{
Ex1Two_Sum ex1;
ex1.f();
return 0;
}
I compile as follows:
g++ -std=c++11 -c Ex1Two_Sum.cpp
g++ -std=c++11 -c main.cpp
g++ Ex1Two_Sum.o main.o
yielding the following message:
main.o: In function `main':
main.cpp:(.text+0x2c): undefined reference to `ddc::Ex1Two_Sum::f()'
collect2: error: ld returned 1 exit status
Your source file redefines the whole class, with an inline function definition, when it just needs to provide a non-inline function definition.
#include "Ex1Two_Sum.h"
void ddc::Ex1Two_Sum::f() {
std::cout << "should work\n";
}
Also, please don't put using namespace std; in a header. Not everyone wants the global namespace polluted in potentially surprising ways.
First, which line of the command throws that error?
Second, I think you forgot to include the Ex1Two_Sum.h in the Ex1Two_Sum.cpp
Third you need to change class ....... in Ex1Two_Sum.cpp to:
void Ex1Two_Sum::f(){...}

Undefined reference to files [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 7 years ago.
I'm having an undefined reference error in my main. Not sure what I'm doing wrong and have tried changing names and moving things around but keep getting the same error. I'm wondering if maybe it's my IDE but really don't know
Here's the code:
#include <iostream>
#include "f.h"
#include "g.h"
using namespace std;
int main()
{
F f;
G g;
f.f();
g.g();
return 0;
}
next file:
#ifndef F_H_INCLUDED
#define F_H_INCLUDED
class F
{
public:
void f();
};
#endif
next file:
#ifndef G_H_INCLUDED
#define G_H_INCLUDED
class G
{
public:
void g();
};
#endif
next file:
#include "f.h"
#include <iostream>
void F::f()
{
std::cout << "This was function f!" << std::endl;
}
next file:
#include "g.h"
#include <iostream>
void G::g()
{
std::cout << "This was function g!" << std::endl;
}
edit: so i changed the include from "f.h" and "g.h" to "f.cpp" and "g.cpp" and now it works... can anyone explain why?
It's looks fine, but make sure if your files names are correct, there is difference between lower and upper case.
#include "f.h"
#include "g.h"
its not the same as
#include "F.h"
#include "F.h"
I test it with VS2013, it is right, if you copy code, please check it.

"undefined reference to" while i am trying to inherit vector class [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 9 years ago.
Main.cpp
#include <iostream>
#include "include/Numbers.h"
#include <vector>
#include <string>
#include <fstream>
using namespace std;
int main()
{
ofstream myofile;
ifstream myifile;
myofile.open("output.txt");
myifile.open("input.txt");
int number;
Numbers input;
if(myifile.is_open())
while(myifile >> number) {
input.push_back(number);
}
cout << input.size() << endl;
myofile.close();
myifile.close();
cout << "Hello world!" << endl;
return 0;
}
Numbers.h
#ifndef NUMBERS_H
#define NUMBERS_H
#include <vector>
class Numbers: public std::vector<int>
{
public:
Numbers();
~Numbers();
int size();
Numbers prob();
protected:
private:
};
#endif // NUMBERS_H
Numbers.cpp
#include "../include/Numbers.h"
#include <iostream>
using namespace std;
Numbers::Numbers()
{
}
Numbers::~Numbers()
{
//dtor
}
I am trying to create a new Numbers class which inherits functions from vector class.
The error I am getting is undefined reference to 'Numbers::size()' although the push_back function didn't give any problem
I am using codeblocks to write my code, and I have included all files in the build properties
First, what you are doing there is not a good idea. It is generally not intended to use STL-classes as base-classes (except for those especially designed for this, such as std::unary_function). std::vector does not have any virtual methods, so it does not have any value as a public base-class. Making things worse, std::vector does not even have a virtual destructor, so when you use it polymorphically, you will probably create a memory leak:
void deleteVector(std::vector<int>* v)
{
delete v;
}
deleteVector( new Numbers() );
In your case, you declared a Method Numbers::size which is not defined in the source-file. You could use a using declaration to use the size-method of your base-class, but that is not needed for public methods.
class Numbers: private std::vector<int>
{
public:
using std::vector<int>::size;
};

Undefined reference from templated function (C++) [duplicate]

This question already has answers here:
Why can templates only be implemented in the header file?
(17 answers)
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 10 years ago.
main.o: In function `main':
main.cpp:(.text+0x2f): undefined reference to `Foo<int>::display(int)'
collect2: ld returned 1 exit status
caused by
g++ -c *.cpp && g++ *.o -o foo
with foo.hpp:
#ifndef FOO_H_
#define FOO_H_
template<typename T>
class Foo {
private:
T ft_;
public:
Foo(const T & ft) : ft_(ft) { }
Foo display(T x);
};
#endif
foo.cpp:
#include "foo.hpp"
#include<iostream>
using namespace std;
template<typename T>
Foo<T> Foo<T>::display(T x) {
// do some stuff - not too relevant
cout << "[" << x << "]";
Foo<T> res(x);
return res;
}
and main.cpp:
#include<iostream>
#include "foo.hpp"
using namespace std;
int main() {
Foo<int> f(42);
Foo<int> g = f.display(39);
return 0;
}
Why?!
P. S. Works with inlined function definitions. Troubles come whenever declaration and definition of a function is split into two files...
In C++ you need to put the definition of templated methods and functions into the header file.