base1.h
void base2::base22fun()
{
cout<<"inside base2::base22fun()"<<endl;
}
base1.cpp
#include "base1.h"
#include "iostream"
using namespace std;
void base1::base1fun()
{
cout<<"inside base1::base1fun()"<<endl;
}
base2.h
class base2
{
public:
virtual void base2fun();
};
base2.cpp
#include "base2.h"
#include "iostream"
using namespace std;
void base2::base2fun()
{
cout<<"inside base2::base2fun()"<<endl;
}
derived.h
#include "base1.h"
#include "base2.h"
class derived : public base1, public base2
{
public:
virtual void base1fun();
virtual void base2fun();
};
derived.cpp
#include "derived.h"
#include "iostream"
using namespace std;
void derived::base1fun()
{
cout<<"inside derived::base1fun"<<endl;
}
void derived::base2fun()
{
cout<<"inside derived::base2fun"<<endl;
}
global.cpp
#include "derived.h"
static derived d;
base1& b1=d;
base2& b2=d;
main.cpp
#include "base2.h"
#include "iostream"
using namespace std;
int main()
{
extern base2& b2;
cout<<b2.base2fun();
return 0;
}
I generated object file of all .cpp files using g++ base1.cpp base2.cpp derived.cpp global.cpp main.cpp -c
Then I linked them all, it worked fine.
Now I modified base2.h base2.cpp and main.cpp as follows
base2.h
class base2
{
public:
int padding;
virtual void base22fun();
virtual void base2fun();
};
base2.cpp
#include "base2.h"
#include "iostream"
using namespace std;
void base2::base22fun()
{
cout<<"inside base2::base22fun()"<<endl;
}
void base2::base2fun()
{
cout<<"inside base2::base2fun()"<<endl;
}
main.cpp
#include "base2.h"
#include "iostream"
using namespace std;
int main()
{
extern base2& b2;
cout<<b2.padding;
return 0;
}
I then recompiled base2.cpp, derived.cpp and main.cpp
I didn't recompile global.cpp, and used the old object file[global.o], and g++ linked them and the executable executed. How is this possible?
Thanks.
First of all, learn to use a makefile. That way, you don't have to type so much...
The linker will succeed as long as the required global symbols are present - in this case, the constructor of the class and probably the vtable of the class. Your object takes up extra space after the recompile, so it will overwrite another variable. If you were to add:
static int x = 42;
after static derived d; - and not initialize padding in the constructor, you'd see padding printed as 42.
And all of this is "undefined behaviour". It is allowed to fail in any plausible way - format your hard-disk, start world war III, or "kind of work with subtle side-effects". Using a makefile with the relevant dependencies set so that you automatically recompile objects.cpp whenever base2.h changes would be the right thing to do.
the object "static derived d;" is created when you run your exe. it has nothing to do with complie and link. so it worked.
Related
Why does codeblocks give this error "Undefined reference to class::classfunction()"
It happens when a class is created in a separated file.All of these files are in the same folder
This is the main .cpp file
#include<iostream>
#include "Class2.h"
using namespace std;
main()
{
Class2 classObject;
cout<<"I'm class2"<<endl;
}
class header file
#ifndef CLASS2_H
#define CLASS2_H
class Class2
{
public:
Class2();
~Class2();
protected:
private:
};
#endif // CLASS2_H
class cpp file
#include "Class2.h"
#include<iostream>
using namespace std;
Class2::Class2()
{
cout<<"Hello, I'm Constructor"<<endl;
}
Class2::~Class2()
{
cout<<"Yo!! I'm Destructor"<<endl;
}
error is "undefined reference to Class2::Class2()"
You need to link both main.o and class.o into your executable. The exact command depends on your compiler and OS. For g++ the command would look something like
g++ -o main main.cpp class.cpp
Why does codeblocks give this error "Undefined reference to class::classfunction()"
It happens when a class is created in a separated file.All of these files are in the same folder
This is the main .cpp file
#include<iostream>
#include "Class2.h"
using namespace std;
main()
{
Class2 classObject;
cout<<"I'm class2"<<endl;
}
class header file
#ifndef CLASS2_H
#define CLASS2_H
class Class2
{
public:
Class2();
~Class2();
protected:
private:
};
#endif // CLASS2_H
class cpp file
#include "Class2.h"
#include<iostream>
using namespace std;
Class2::Class2()
{
cout<<"Hello, I'm Constructor"<<endl;
}
Class2::~Class2()
{
cout<<"Yo!! I'm Destructor"<<endl;
}
error is "undefined reference to Class2::Class2()"
You need to link both main.o and class.o into your executable. The exact command depends on your compiler and OS. For g++ the command would look something like
g++ -o main main.cpp class.cpp
I'm very new to c++ and to my understanding if i include the header file which is where the functions are defined, i should be able to call the functions from main? I have tried to add static or public before the functions but nothing seemed to change.
//heres my main
#include "pch.h"
#include "myfile1.h"
#include <iostream>
using namespace std;
int main(void){
function1();
function2();
}
//my myfile1 header
#ifndef myfile1_h
#define myfile1_h
#include <iostream>
using namespace std;
class myClass{
void function1();
void function2();
};
#endif
//my myfile1.ccp
#include "myfile1.h"
#include <iostream>
using namespace std;
class myClass{
void function1() {
}
void function2() {
}
}
Your functions are in the myClass in myfile1. You must create object from this class, and you can use functions where in the this class. My English skills are not good but you must do this:
int main(void){
myClass myClassObject;
myClassObject.function1();
myClassObject.function2();
}
If you dont want create object from this class, you can do that :
myfile1.h:
#ifndef myfile1_h
#define myfile1_h
#include <iostream>
using namespace std;
void function1();
void function2();
#endif
myfile1.cpp:
#include "myfile1.h"
void function1(){
cout<<"function1"<<endl;
}
void function2(){
cout<<"function2"<<endl;
}
and main:
#include "myfile1.cpp"
int main(){
function1();
function2();
return 0;
}
If you want this functions with class, you can do it for good case:
myfile1.h
#ifndef myfile1_h
#define myfile1_h
#include <iostream>
using namespace std;
class myClass{
public: // If you want access these functions, you must use public tag.
// If you dont use any tag, it will be private, because default tag is private.
void function1();
void function2();
};
#endif
myfile1.cpp
#include "myfile1.h"
void myClass::function1(){
cout<<"function1";
}
void myClass::function2(){
cout<<"function2";
}
main:
#include "myfile1.h"
int main(){
myClass myClassObject;
myClassObject.function1();
myClassObject.function2();
return 0;
}
What is your compiler or editor? If you compile that codes from terminal, you must compile it like this:
g++ main.cpp myfile1.cpp
My English skills are not good. But I tried to explain.
all! I am trying to create a very simple inheritance structure using C++ and header files but (of course) I am having some difficulties.
When I try to compile my main program, I get this error:
In function `Base::Base()':
undefined reference to 'vtable for Base'
In function `Derived::Derived()':
undefined reference to 'vtable for Derived'
All I want is to print is
printed in Derived
but I am having some extreme difficulties.
Here are my program files:
main.cpp
#include <iostream>
#include "Base.h"
#include "Derived.h"
using namespace std;
int main(void) {
Base *bp = new Derived;
bp->show();
return 0;
}
Base.cpp
#include <iostream>
#include "Base.h"
virtual void Base::show() {
cout << "printed in Base";
}
Base.h
#ifndef BASE_H
#define BASE_H
class Base {
public:
virtual void show();
};
#endif
Derived.cpp
#include <iostream>
#include "Derived.h"
using namespace std;
void Derived::show() override {
cout << "printed in Derived";
}
Derived.h
#ifndef DERIVED_H
#define DERIVED_H
class Derived: public Base {
public:
void show() override;
};
#endif
Thank you! And any help is very much appreciated!...extremely.
As pointed out in the comments, by calling g++ main.cpp you are only compiling main.cpp.
You need to compile all files, then link them together. If you do so, you will see that there are compilation issues in your other cpp files as also pointed out in the comments (virtual and override only belong in the header).
So you need to call the following to compile all files:
g++ main.cpp Base.cpp Derived.cpp -o myapp
Why does codeblocks give this error "Undefined reference to class::classfunction()"
It happens when a class is created in a separated file.All of these files are in the same folder
This is the main .cpp file
#include<iostream>
#include "Class2.h"
using namespace std;
main()
{
Class2 classObject;
cout<<"I'm class2"<<endl;
}
class header file
#ifndef CLASS2_H
#define CLASS2_H
class Class2
{
public:
Class2();
~Class2();
protected:
private:
};
#endif // CLASS2_H
class cpp file
#include "Class2.h"
#include<iostream>
using namespace std;
Class2::Class2()
{
cout<<"Hello, I'm Constructor"<<endl;
}
Class2::~Class2()
{
cout<<"Yo!! I'm Destructor"<<endl;
}
error is "undefined reference to Class2::Class2()"
You need to link both main.o and class.o into your executable. The exact command depends on your compiler and OS. For g++ the command would look something like
g++ -o main main.cpp class.cpp