why do the following member initializers not work in c++ [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 3 years ago.
I'm learning C++ following this link: https://www.youtube.com/watch?v=mUQZ1qmKlLY. Now I'm confused about member initialization. So, I have three files, main.cpp, Sally.h, Sally.cpp. as the following.
main.cpp
#include <iostream>
#include "Sally.h"
using namespace std;
int main(){
Sally so(3,87);
so.print();
}
Sally.h
#ifndef SALLY_H
#define SALLY_H
class Sally
{
public:
Sally(int a, int b);
void print();
private:
int regVar;
const int constVar;
};
#endif
Sally.cpp
#include "Sally.h"
#include <iostream>
using namespace std;
Sally::Sally(int a, int b)
:regVar(a), constVar(b)
{
}
void Sally::print()
{ cout << "regulat var is: " << regVar << "const var is:" << constVar << endl;
}
When I run the main.cpp file, it does not give me any prints out. Instead, it gives me the following message.
$ g++ main.cpp
/tmp/ccyvg9rV.o: In function `main':
main.cpp:(.text+0x29): undefined reference to `Sally::Sally(int, int)'
main.cpp:(.text+0x35): undefined reference to `Sally::print()'
collect2: error: ld returned 1 exit status
Moreover, why do not I see anything similar to this member initialization in other languages, like Java, Julia or Python?

When I run the main.cpp file
g++ main.cpp
That does not run the file, it compiles and links it.
Since main.cpp is not a full program (you also need Sally.cpp), the linker tells you you have undefined references.
Instead, do:
g++ Sally.cpp main.cpp
And you should get a binary/executable in your current folder that you can run.

Related

Preventing multiple definition in C++ [duplicate]

This question already has answers here:
multiple definition error c++
(2 answers)
Closed 1 year ago.
I am getting error:
/usr/bin/ld: /tmp/ccCbt8ru.o: in function `some_function()':
Thing.cpp:(.text+0x0): multiple definition of `some_function()'; /tmp/ccc0uW5u.o:main.cpp:(.text+0x0): first defined here
collect2: error: ld returned 1 exit status
when building a program like this:
main.cpp
#include "common.hpp"
#include "Thing.hpp"
int main() {
some_function();
}
common.hpp
#pragma once
#include <iostream>
void some_function() {
std::cout << "something" << std::endl;
}
Thing.hpp
#pragma once
class Thing {
public:
void do_something();
};
Thing.cpp
#include "Thing.hpp"
#include "common.hpp"
void Thing::do_something() {
some_function();
}
I'm compiling with: g++ main.cpp Thing.cpp -o main.out
I've also tried using include guards instead of #pragma once, but it didn't seem to work either. Is there something I am forgetting here?
#pragma once and include guards can only prevent multiple definitions in a single translation unit (a .cpp file). The individual units know nothing about each other, and so cannot possibly remove the multiple definition. This is why when the linker links the object files, it sees the multiple definitions still.
To solve this issue, change common.hpp to:
#pragma once
void some_function();
This tells the compiler that there is some code for some_function.
Then, add a file common.cpp that contains the code for the common.hpp header:
#include "common.hpp"
#include <iostream>
void some_function() {
std::cout << "something" << std::endl;
}
Finally, change the g++ command to:
g++ main.cpp common.cpp Thing.cpp -o main

Undefined reference to function using Class header [duplicate]

This question already has answers here:
Code-runner configuration for running multiple cpp classes in vscode
(1 answer)
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 1 year ago.
I've been following tutorial on how to create class using header files and came to a problem even if I did everything like in tutorial. I got Cat.h, Cat.cpp and main.cpp files. All of them are in the same folder.
Cat.h:
#ifndef CAT_H_
#define CAT_H_
class Cat
{
public:
void speak();
};
#endif
Cat.cpp:
#include <iostream>
#include "Cat.h"
using namespace std;
void Cat::speak()
{
cout << "Meeeow!" << endl;
}
main.cpp:
#include <iostream>
#include "Cat.h"
using namespace std;
int main()
{
Cat jim;
jim.speak();
return 0;
}
When i run the program I got error: "undefined reference to `Cat::speak()'". The problem is solved when i add line #include "Cat.cpp" to main.cpp but I dont think thats a way to go and tutorial was done without that.
Solved. For anyone having the same problem using VS Code with Code Runner extension, I found the solution in different thread:
Code-runner configuration for running multiple cpp classes in vscode

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!

Undefined reference to sample void 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)
Closed 6 years ago.
well I want to understand linking with headers and other .cpp (functions for example) so my quastion is why I get "undefined reference to 'afis(). There are sample example and I want to clarify this. Also sorry for my bad english :D.
There is main:
#include <iostream>
#include "functions.h"
using namespace std;
int main()
{
afis();
return 0;
}
There is an function named function.cpp:
#include <iostream>
using namespace std;
void afis(){
cout <<"yehe";
}
And there is the header :
#ifndef FUNCTIONS_H_INCLUDED
#define FUNCTIONS_H_INCLUDED
void afis();
#endif // FUNCTIONS_H_INCLUDED
While the C++ compiler automatically "pulls in" referenced header files, it can't do that for the actual .cpp code files.
Instead of calling
CXX/clang++/g++ main.cpp -o hello
you need to manually include all relevant code files:
CXX/clang++/g++ main.cpp functions.cpp -o hello

Error: undefined reference to `censorship()' [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What is an undefined reference/unresolved external symbol error and how do I fix it?
I have main.cpp:
#include "censorship_dec.h"
using namespace std;
int main () {
censorship();
return 0;
}
this is my censorship_dec.h:
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
void censorship();
this is my censorship_mng.cpp:
#include "censorship_dec.h"
using namespace std;
void censorship()
{
cout << "bla bla bla" << endl;
}
I tried to run these files in SSH (Linux), so I wrote: make main, but I got:
g++ main.cpp -o main
/tmp/ccULJJMO.o: In function `main':
main.cpp:(.text+0x71): undefined reference to `censorship()'
collect2: ld returned 1 exit status
make: *** [main] Error 1
please help!
You have to specify the file where censorship is defined.
g++ main.cpp censorship_mng.cpp -o main
You must add censorship_mng.cpp in your compilation command:
g++ main.cpp censorship_mng.cpp -o main
Another solution (if you really don't want change your compile command) is making void censorship(); to a inline function and move it from .cpp to .h.
censorship_dec.h:
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
inline void censorship()
{
// your code
}
And remove void censorship() from censorship_mng.cpp file.
once your project starts using several source-files to be compiled into a single binary, manual compilations become tedious.
this is usually the time when you start using a build-system, such as a Makefile
a very simple Makefile that uses default build-rules could look like
default: main
# these flags are here only for illustration purposes
CPPFLAGS=-I/usr/include
CFLAGS=-g -O3
CXXFLAGS=-g -O3
LDFLAGS=-lm
# objects (.o files) will be compiled automatically from matching .c and .cpp files
OBJECTS=bar.o bla.o foo.o main.o
# application "main" build-depends on all the objects (and linksthem together)
main: $(OBJECTS)