"Undefined symbols for architecture arm64" - What does this mean? [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 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>

Related

Undefined reference to function of 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 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.

Issue with circular dependency even after separating definitions [duplicate]

This question already has answers here:
Resolve build errors due to circular dependency amongst classes
(12 answers)
Closed 7 years ago.
Please see my previous post here:
Undefined type error even with forward declaration
I moved the definitions to cpp files and I still face the issue. Any ideas why? My files look like this:
Header1.hpp
#ifndef HEADER1_HPP
#define HEADER1_HPP
namespace sample_ns
{
class sample_class{
public:
static int getNumber();
static void print();
};
}
#endif
Header2.hpp
#ifndef HEADER2_HPP
#define HEADER2_HPP
namespace sample_ns
{
class sample_class2{
public:
sample_class2();
int getNumber2();
};
}
#endif
Source1.cpp
#include "Header1.hpp"
#include "Header2.hpp"
#include "stdafx.h"
#include <iostream>
namespace sample_ns
{
int sample_class::getNumber()
{
sample_class2 obj;
return obj.getNumber2();
}
void sample_class::print()
{
std::cout << "Print utility function" << std::endl;
}
}
Source2.cpp
#include "Header2.hpp"
#include "Header1.hpp"
#include "stdafx.h"
#include <iostream>
namespace sample_ns
{
sample_class2::sample_class2()
{
sample_class::print();
}
int sample_class2::getNumber2()
{
sample_class::print();
return 5;
}
}
In my main I call it as:
std::cout << sample_ns::sample_class::getNumber() << std::endl;
I get 'sample_class2' : undeclared identifier. I tried adding class sample_class2; but that still gives me error
EDIT:
my main file:
#include "stdafx.h"
#include <iostream>
#include "Header1.hpp"
int _tmain(int argc, _TCHAR* argv[])
{
std::cout << "hello" << std::endl;
std::cout << sample_ns::sample_class::getNumber() << std::endl;
return 0;
}
The best practice for declaring classes and namespaces in header and cpp files is using structure likes below:
Header1.hpp
#ifndef HEADER1_HPP
#define HEADER1_HPP
#include "Header2.hpp"
#include <iostream>
namespace sample_ns
{
class sample_class{
public:
static int getNumber();
static void print();
};
}
#endif
Source1.cpp
#include "Header1.hpp"
namespace sample_ns
{
int sample_class::getNumber()
{
sample_class2 obj;
return obj.getNumber2();
}
void sample_class::print()
{
std::cout << "Print utility function" << std::endl;
}
}
So by including in header files and using ifndef you become sure that circular dependencies will not occure.

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;
};

unresolved external symbol "int randomNumber" [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
When to use extern in C++
(4 answers)
Closed 8 years ago.
Can please someone explain me how to link the functions # functions.cpp to main.cpp
note: I want both files functions.cpp and main.cpp to use the same variables from header.h
Thank you!
main.cpp
#include "Header.h"
#include <iostream>
using namespace std;
int multi();
int printOutRanomdNumber();
int main()
{
cout << "Eneter a number you want to multiply" << endl;
cout << multi() <<endl;
cout << printOutRanomdNumber();
system("pause");
return 0;
}
header.h
#ifndef _HEADER_
#define _HEADER_
#include <iostream>
using namespace std;
extern int randomNumber;
int multi();
int printOutRanomdNumber();
#endif
functions.cpp
#include "Header.h"
#include <iostream>
using namespace std;
int multi()
{
int x;
cin >> x;
return(x=x*x);
}
int printOutRanomdNumber()
{
cout << "Please enter a random number" << endl;
cin >> randomNumber;
return (randomNumber);
}
The error is because you've not defined int randomNumber in any of your files.
You need to define randomNumber in one of the .cpp files, I'm guessing functions.cpp makes more sense here.
Also you can get rid of these lines in main.cpp since you're including Header.h which provides the prototypes already.
int multi();
int printOutRanomdNumber();