unresolved external symbol "int randomNumber" [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)
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();

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>

How to use extern to link to a function in other cpp file?

Here is my main.cpp:
#include <iostream>
#include "function.cpp"
using namespace std;
extern int giveMain();
int main() {
int x = 4;
x = giveMain(x);
cout << x << endl;
}
And here is my function.cpp:
#include <iostream>
using namespace std;
int giveMain(int a) {
a = 3 + a;
return a;
}
But when I compile, it says that "Linker command failed". Can anyone helps me to solve this problem.
You declared the function int giveMain() in main.cpp but the function in function.cpp takes an int. Declare the correct function and it should work. Also extern is the default for functions so you don't need to include the keyword.
EDIT: Just noticed that you #include <function.cpp> in main.cpp. Never include .cpp files. The issue you were having was multiple definitions for int giveMain(int) because the contents of functions.cpp was being compiled twice.

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.

Multiple definition of function in C++

I am having a very unusual problem:
I keep getting multiple definition of functions in my class.
This is my main .cpp
#include <iostream>
#include "Calculation.cpp"
using namespace std;
int main()
{
cout << "Hello world!" << endl;
return 0;
}
this is my class .h
#ifndef CALCULATION_H_INCLUDED
#define CALCULATION_H_INCLUDED
class Calculation
{
public:
Calculation();
private:
};
#endif // CALCULATION_H_INCLUDED
this is my implementation file .cpp
#include "Calculation.h"
Calculation::Calculation()
{
}
Please help me; I have tried to create a new project but that didn't help.
All help is appreciated.
make your main.cpp like :
#include <iostream>
#include "Calculation.h" // not Calculation.cpp
using namespace std;
int main()
{
cout << "Hello world!" << endl;
return 0;
}
You have to include your Calculation.h in th main.cpp and you have to compile it as follows,
g++ main.cpp Calculate.cpp -o main -I<path for your .h file>
main.cpp
#include<iostream>
#include "Calculation.h"
//using namespace std; // Avoid this, always to use std::cout .. etc on place
int main()
{
Calculation c; //Creating the object of Calculation class
std::cout<<"Hello World!"<<std::endl;
return 0;
}

Undefined reference to 'WinMain#16'

I am getting a [Linker Error] undefined reference to 'WinMain#16' and I am unable to fix the issue. I am using Dev-C++ - In my project settings 'Win32 Console' is selected as I want it to be a console application.
Example Header (Test.h):
#ifndef TEST_H
#define TEST_H
#include<string>
using namespace std;
class Test {
private:
int testing;
public:
int main();
};
#endif
Example .cpp file
#include<iostream>
#include "Test.h"
using namespace std;
int Test::main(){
/* EXAMPLE */
cout << "Enter Test" <<endl;
cin >> testing;
cout << "----------------------------"<<endl;
system("pause");
return 0;
}
I can fix the error by removing the Test:: in front of the main() but I want it to reference my header file. If it does not reference my header file all of my variables become undeclared.. unless I put them into the program itself.
Please note the code is only an example of what I am doing.. and sorry once again if it's stupidly obvious. :-(
Answers are provided in the comments themselves, but here is the gist::
#ifndef TEST_H
#define TEST_H
#include<string>
using namespace std;
class Test {
private:
int testing;
public:
int main();
};
int Test::main(){
/* EXAMPLE */
cout << "Enter Test" <<endl;
cin >> testing;
cout << "----------------------------"<<endl;
system("pause");
return 0;
}
#endif
And in the .cpp file::
#include<iostream>
#include "Test.h"
using namespace std;
int main(){
/* EXAMPLE */
Test *testObject = new Test();
testObject->main();
delete(testObject);
system("pause");
return 0;
}
Oh, and why are you using system("PAUSE"), when there is a much better way!? (You can read here as to why system() is evil: http://www.cplusplus.com/forum/articles/11153/ )
Why not go with something like this:
void PressEnterToContinue()
{
std::cout << "Press ENTER to continue...\n";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
and then call the function at the end??