I am hoping someone can point me in the right direction to figure out why I am getting the following error:
$~/display/triangleDisplayable.cc:4: undefined reference to `Displayable::Displayable()'
I am trying to abstract a class Displayable and have a class triangleDisplayable that implements its methods. The two header files I have are "Displayable.h":
class Displayable {
public:
Displayable();
virtual int getSizeOfArrays() = 0;
void display(int size);
private:
virtual void init() = 0;
virtual int getSizeOfPointsArray() = 0;
virtual int getSizeOfNormalsArray() = 0;
};
and "triangleDisplayable.h"
#include "Displayable.h"
class triangleDisplayable : public Displayable
{
public:
triangleDisplayable();
int getSizeOfArrays();
private:
void init();
int getSizeOfPointsArray();
int getSizeOfNormalsArray();
};
And then I have "Displayable.cc"
#include <iostream>
#include "Displayable.h"
Displayable::Displayable() {
std::cout << "testing Displayable constructor" << std::endl;
}
void Displayable:display(int size) {
}
int main () {
return 0;
}
and "triangleDisplayable.cc"
#include <iostream>
#include "triangleDisplayable.h"
triangleDisplayable::triangleDisplayable() : Displayable() {
}
int triangleDisplayable::getSizeOfArrays() {
return 0;
}
void triangleDisplayable::init() {
}
int triangleDisplayable::getSizeOfPointsArray() {
return 0;
}
int triangleDisplayable::getSizeOfNormalsArray() {
return 0;
}
int main () {
return 0;
}
I have been trying to follow along with various tutorials to learn how to do abstraction in C++, but I have not really been able to find any helpful solutions to this. I believe that all of my #includes are correct, which I read is a common problem. The error message seems to indicate that the problem is the line
triangleDisplayable::triangleDisplayable() : Displayable() {
}
I have tried to compile without the : Displayable() but I get the same error. Is there perhaps a problem with my syntax in my header files?
No, the error is in tool invocation. You need to link the two source files together (e.g. g++ -o foo a.cc b.cc). And remove one of the main functions, as you can't have two different ones.
Related
I am so confused right now...
Here is the code that I will be talking about:
main.cpp:
#include "CustomVector.h"
#include <iostream>
int main() {
CustomVector<int, 15> hello{};
std::cout << hello.size() << '\n';
return 0;
}
CustomVector.h:
#pragma once
template<typename T, int S>
class CustomVector {
private:
T arr[S];
int size;
public:
CustomVector() : arr{}, size{ S } {}
// Methods
int size() const {
return size;
}
};
As you can see I am trying to use the size() method that I have in my class definition, which there shouldn't be a problem with, right..? But when I try to use it in main.cpp and try to print it out to the console, it is giving me the compiler error which my question title has. Why could this be, this has never happened to me before. I would think there is some sort of "redefinition" somehow, but how could that be, there couldn't be a redefinition if it is in my own user-defined class?
There is a theme - Separating class code into a header and cpp file
It describes how to separate a class with variables and methods to .h and .cpp
But it's a simple one.
Say I have this in main.cpp
int main() {
class Filth {
int a, b;
void pra() { std::cout << a; }
class Frank {
int sacrifices;
void praisChinChin() { std::cout << "DARK LORD IS COMMINGGGGGG"; }
}
};
}
And how do I write THIS class (Filth) into a .h and .cpp so I dont get "undefined reference" and any other mistake?
And how exactly does it work (why I should write this exact code, what exactly does it do to my program)?
frank.cpp
#include "frank.h"
#include <iostream>
void Frank::praisChinChin() {
std::cout << "DARK LORD IS COMMINGGGGGG";
}
frank.h
#pragma once
class Frank {
int sacrifices = 0;
public:
void praisChinChin();
};
filth.cpp
#include "filth.h"
#include <iostream>
void Filth::pra() {
std::cout << a;
}
filth.h
#pragma once
class Filth {
int a = 0;
int b = 0;
void pra();
};
test.cpp
#include "frank.h"
int main() {
Frank f;
f.praisChinChin();
}
You are missing a semi colon at the end of class Frank.
It should compile after that.
To separate the class into .h and .cpp file you should make you function non local to the main function.
Header file might look like this.
class Filth
{
int a, b;
void pra();
class Frank
{
int sacrifices;
void praisChinChin();
};
};
And the cpp file
void Filth::pra()
{
std::cout << a;
}
void Filth::Frank::praisChinChin()
{
std::cout << "DARK LORD IS COMMINGGGGGG";
}
int main()
{
return 0;
}
I'm not sure about the "why should I write the exact code". But at the moment your code is not really doing anything. You need to create objects of your classes and call member functions, for it to have any real effect.
How can such a code work correctly when the IWindow pointer clearly has an address to a ISheet class which has no method Say?
#include <iostream>
using namespace std;
class IWindow
{
private:
int p;
double f;
public:
void Say() { cout << "Say in IWindow"; }
};
class ISheet
{
public:
void foo() { cout << "ISheet::foo"; }
};
int main()
{
ISheet *sh = new ISheet();
int ptr = (int)sh;
IWindow *w = (IWindow*)ptr;
w->Say();
sh->foo();
return 0;
}
When compiled in Visual Studio 2015 it runs and executes with no problems, but I was expecting to get an error on line w->Say(). How is this possible?
It works by the grace of the almighty Undefined Behavior. Your functions don't try to access any data members of the containing class, they just write something to std::cout, which anyone can do.
What you've effectively done is
#include <iostream>
void IWindow_Say(void*)
{
std::cout << "Say in IWindow";
}
int main()
{
IWindow_Say(0xdeadbeef); // good luck with that pointer
}
You never used the pointer (which became this in your original example) so no side-effects were observed.
I have a simple class which I cannot instantiate and I don't know why...
Please help me !
-------Test.cpp-------
#include<iostream>
using namespace std;
#include "meteo.h"
int main()
{
Meteo meteo;
}
-------meteo.h---------
#ifndef METEO_H
#define METEO_H
class Meteo
{
public:
Meteo();
int Get(int i);
private:
char *list[];
};
#endif
-------meteo.cpp--------
#include "meteo.h"
Meteo::Meteo()
{
list[]("Sec","Venteux","Humide");
}
int Meteo::Get(int i)
{
return list[i];
}
I get the error: "undefined reference to `Meteo::Meteo()'"
It seems that the problem is that the compiler issued an error when was compiling the constructor
Meteo::Meteo()
{
list[]("Sec","Venteux","Humide");
}
and did not generate the object module.
This record
list[]("Sec","Venteux","Humide");
is invalid.
Try to change the class definition like
class Meteo
{
public:
Meteo();
int Get(int i);
private:
const char *list[3];
};
and define the constructor like
Meteo::Meteo() : list { "Sec","Venteux","Humide" }
{
}
The other reason might be that you did not include object module meteo in the project.
Take into account that this member function
int Meteo::Get(int i)
{
return list[i];
}
is also wrong. The type of elements of the array is const char * not int.
I am learning and testing a piece of C++ code as follows:
#include "stdafx.h"
#include <iostream>
using namespace std;
#include <conio.h>
#include <cstring>
class Shape {
public:
Shape() {};
~Shape() {};
virtual void display() const = 0;
virtual double volume() const = 0;
};
class Square : public Shape {
public:
Square() {};
~Square() {};
void display() const;
double volume() const;
};
void Square::display() const {
cout << "Square!!!!!!!!!!!!!!" << endl;
}
double Square::volume() const {
cout << "Square Volume........." << endl;
return 0.0;
}
int _tmain(int argc, _TCHAR* argv[])
{
Shape *s;
s = new Square; // error here
(*s).display();
return 0;
}
The above code does not compile successfully. it produces: "fatal error LNK1120: 1 unresolved externals".
Can anyone help me out with that?
I am using MS VS C++ 2005.
Thanks
The above code compiles and runs properly on VS 2010 as well as Ideone.
Check this
There is nothing wrong in the way you have implemented your abstract functions in the above snippet.
I'm pretty sure your problem is your main declaration.
If you change it to a standard main definition, I believe your linking problems will be fixed.
int main()
{
Shape *s = new Square(); // error here
s->display();
return 0;
}