Creating a class in C++ error - c++

I have a test class, it has nothing in it apart from the bones of a class.
h:
#ifndef TEST_H_
#define TEST_H_
class Test
{
public:
Test();
};
#endif /* TEST_H_ */
cpp
#include "test.h"
Test::Test()
{
}
And then in my main class I have:
Test *test = new Test();
I also include test.h.
I get the error:
Undefined reference to Test::Test()
Can anyone tell me where i'm going wrong?

Figured it out, even after cleaning the project and restarting the IDE it still wouldn't work. I had to manually add in test.h and test.cpp to my config.pri

Related

Error while using the recursive inclusion of Qt headers [duplicate]

This question already has answers here:
Resolve build errors due to circular dependency amongst classes
(12 answers)
Closed 4 years ago.
I have big project in which appeared extremely incomprehensible mistake, after which I created a new project repeated it:
struct of project is simple:
main.cpp
first.h
second.h
first.h
#ifndef FIRST_H
#define FIRST_H
#include "second.h"
class first
{
public:
first();
};
#endif // FIRST_H
second.h
#ifndef SECOND_H
#define SECOND_H
#include "first.h"
class second
{
public:
second();
};
#endif // SECOND_H
Everything is quite logical, but Qt Creator thinks quite differently:
D:\WinFiles\Documents\QT\DELME1\DELME1\second.h:1: error: unterminated conditional directive
I am a bit at a loss case as for inheritance this construction is needed
Problem solving for MinGW compiler:
//first.h
#ifndef FIRST_H
#define FIRST_H
class second;
class first
{
public:
first();
};
#endif // FIRST_H
//second.h
#ifndef SECOND_H
#define SECOND_H
class first;
class second
{
public:
second();
};
#endif // SECOND_H
The second solution is to use MSVC compiller for qt which will guess what needs to be
done by itself and compiled all of the examples correctly.

c++ inheritance issues: undefined reference to 'vtable'

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

Error C2504: 'Vending': base class undefined

Alright, so I have two classes, Vending and Payment. Payment is the child of Vending. I keep getting the "base class undefined" error in my code.
Here are the two header files:
//Parent class (Vending.h)
#ifndef VENDING_H
#define VENDING_H
#include "Main.h";
namespace Vending
{
class Vending
{
public:
Vending();
Vending(int);
void setRequiredAmount(int);
int getRequiredAmount();
protected:
int selectedItem;
int requiredAmount;
};
}
#endif VENDING_H
//child class (Payment.h)
#ifndef PAYMENT_H
#define PAYMENT_H
#include "Vending.h"
namespace Vending
{
class Payment : public Vending
{
public:
Payment(int);
int getEnteredAmount();
void setEnteredAmount(int);
protected:
int enteredAmount;
};
}
#endif PAYMENT_H
It would be greatly appreciated if I can get some help to resolve this error
You say Main.h includes Payment.h, which DOES lead to circular dependencies. Read this post for additional information: http://forums.codeguru.com/showthread.php?288147-C2504-Base-class-undefined-(other-posts-have-no-solution)&p=919112#post919112
You need to rethink your project properly, conditions like this should not happen. Simply try to remove the #include "Main.h" from the Vending.h, and compile Payment.cpp...

Multiple definition of a function error, even when using #if guard clauses

I am creating a simple UTIL.h file contain aplusb(int, int) function for my C++ project. However I cannot compile and the error message is about multiple definition of `aplusb(int, int)'. Would you please help me correct the error or give me some hints?
I attach here my project for your detail reference.
File UTIL.h
#ifndef UTIL_H_
#define UTIL_H_
int aplusb(int a, int b) {
return a + b;
}
#endif /* UTIL_H_ */
File ClassA.h
#ifndef CLASSA_H_
#define CLASSA_H_
class ClassA {
public:
ClassA();
virtual ~ClassA();
private:
int sum;
};
#endif /* CLASSA_H_ */
File ClassA.cpp
#include "ClassA.h"
#include "UTIL.h"
ClassA::ClassA() {
// TODO Auto-generated constructor stub
sum = aplusb(3,5);
}
ClassA::~ClassA() {
// TODO Auto-generated destructor stub
}
File ClassB.h
#ifndef CLASSB_H_
#define CLASSB_H_
class ClassB {
public:
ClassB();
virtual ~ClassB();
private:
int sum;
};
#endif /* CLASSB_H_ */
File ClassB.cpp
#include "ClassB.h"
#include "UTIL.h"
ClassB::ClassB() {
// TODO Auto-generated constructor stub
sum = aplusb(5,6);
}
ClassB::~ClassB() {
// TODO Auto-generated destructor stub
}
Compile error message
ClassB.o: In function `aplusb(int, int)':
/home/vtvan/Desktop/workspace/commonfunc/UTIL.h:11: multiple definition of `aplusb(int, int)'
ClassA.o:/home/vtvan/Desktop/workspace/commonfunc/UTIL.h:11: first defined here
collect2: error: ld returned 1 exit status
make: *** [commonfunc] Error 1
First variant - use inline specifier
#ifndef UTIL_H_
#define UTIL_H_
inline int aplusb(int a, int b) {
return a + b;
}
#endif /* UTIL_H_ */
Second variant - write definition in .cpp file.
You created the function aplusb in your include file. This means that for every file you include it to, a public function aplusb will be created, resulting in a name clash.
If the function should be inline, then mark it so. If the function should be a template, then mark it so. If the function should be as you wrote it, put it in a cpp file and just keep the protoype in the h file.
.h
#ifndef UTIL_H_
#define UTIL_H_
int aplusb(int a, int b);
#endif
.cpp
int aplusb(int a, int b)
{
return a+b;
}
You should declare your aplusb function in the header file, and provide the definition in a cpp file. Something like
util.h:
#ifndef UTIL_H_
#define UTIL_H_
int aplusb(int, int);
#endif /* UTIL_H_ */
The error message is telling you that each time that you include the util.h file, you are re-defining the function, which is exactly what you are doing :-) This is a violation of the ODR (one-definition-rule), which states that the definition (of a function, in this case) must be unique. Otherwise the compiler would be unable to choose between the alternatives (even if, like in this case, they happen to be equal).
Note that templates complicate the matter a bit (in short, because a template is not a definition until instatiated).
Header files are not intended to have actual functions in them (certain C++ aspects such as templates not withstanding). General practice in your case would have you changing your UTIL.H to just prototyping the function (int aplusb(int a, int b);) and moving its implementation to a source file.
You could also make a Util struct where every function is declared static. You can then access every function using Util::<function name>
File UTIL.h
#ifndef UTIL_H_
#define UTIL_H_
struct Util{
static int aplusb(int a, int b) {
return a + b;
}
};
#endif /* UTIL_H_ */
File ClassA.cpp
#include "ClassA.h"
#include "UTIL.h"
ClassA::ClassA() {
sum = Util::aplusb(3,5);
}
ClassA::~ClassA() {
}
File ClassB.cpp
#include "ClassB.h"
#include "UTIL.h"
ClassB::ClassB() {
sum = Util::aplusb(5,6);
}
ClassB::~ClassB() {
}

How to make classes in NACHOS (C++)

I am trying to implement a player class, so I created two files in my threads folder,
player.cc and player.h
player.h goes like this :
#ifndef PLAYER_H
#define PLAYER_H
#include "utility.h"
class Player()
{
public:
//getPlayerID();
};
#endif
then player.cc goes like
#include "player.h"
class Player()
{
string playerID;
int timeCycle;
}
Then in my main.cc and threadtest.cc , I add in #include player.h and then I start to errors and it fails to compile. I am new to nachos and a little bit unfamiliar with c++, so I am confused as to how to resolve this problem. Nachos does not provide a solution through the compiler either.
When I type gmake, it says two things for errors.
1. parse error before '(' in player.h (referring to Player())
2. * [main.o] Error 1
Let's go through line-by-line:
#ifndef PLAYER_H
#define PLAYER_H
#include "utility.h"
So far so good, you might check if your compiler supports #pragma once, but the macro will work perfectly fine.
class Player()
() aren't allowed in a class name, take them off
{
public:
//getPlayerID();
};
#endif
The rest of the header file is ok. Let's look at the implementation file:
#include "player.h"
Perfect. Putting a class in a header is the best way to make sure you only have one definition used in your whole program.
class Player()
Parentheses aren't allowed, but here you have a bigger problem. You already have a class with that name. Let the header provide the class definition, the implementation file just needs to provide the non-inline member functions (and any helper code).
{
string playerID;
int timeCycle;
}
Here's a complete corrected version:
#if !defined(PLAYER_H)
#define PLAYER_H
#include <string>
#include "utility.h"
class Player
{
std::string player_id;
int time_cycle;
public:
// this is how you make a constructor, the parenthesis belong here, not on the class name
Player(std::string id, int time);
std::string getPlayerId() const;
};
#endif /* !defined(PLAYER_H) */
and implementation file
#include "player.h"
// and this is how you write a non-inline constructor
Player::Player(std::string id, int time)
: player_id(id)
, time_cycle(time)
{}
std::string Player::getPlayerId() const
{
return player_id;
}
All of these problems are really basic C++ stuff, nothing to do with NachOS.
Did you modify the Makefile.common in the root nachos directory? I think you should add some value to THREAD_H, THREAD_O and THREAD_C.