Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I got a problem regarding Eclipse when creating abstract classes. I'm not very used to dealing with header files and such, my code basically looks as follows: (not displaying everything, just the basic class to give an idea of how it looks)
Equipment.h
namespace Equipments {
class Equipment{
public:
virtual ~Equipment();
virtual std::string get_category() const = 0;
protected:
Equipment(std::string name);
private:
const std::string name_;
};
class Weapon : public Equipment {
public:
Weapon(std::string name, std::string something_else);
virtual ~Weapon();
std::string get_category() const override { return "Weapon"; };
private:
const std::string something_else_;
};
} //end of namespace
Now, I got a problem with both the .h and .cpp file
in the .h file, under the weapon constructor, I'm used to writing (since I don't use header files):
Weapon(std::string name, std::string something_else)
: Equipment{name}, something_else_{something_else}
{}
Since I can't do this really (to my knowledge), how do I send parameters to my parent class? (in this case, letting eclipse know that I want my name parameter sent to Equipment parent class)
Should I do it in the .cpp file and if so, how?
And now the second problem.
In the .cpp file I create my equipment class like this:
namespace Equipments {
Equipment::Equipment(std::string name) {
name_ = name;
}
Equipment::~Equipment() {
}
std::string Equipment::get_name()
{
return name_;
}
//etc
But I can't seem to create my Weapon class. If I try:
Equipment::Weapon(std::string name, std::string something_else)
I just get member decleration not found, and if I try:
Weapon::Weapon(std::string name, std::string something_else)
I get no matching function for call to 'Equipments::Equipment::Equipment()'
I'm just stuck with not knowing how eclipse want me to write my code, I know it's a bit of a noob problem but I haven't been using either header files or c++ in eclipse for quite a long time. I'm really close to just pick my laptop and program in ubuntu and gedit instead so I don't have to deal with the header classes, however, then I won't learn anything either.
But I can't seem to create my Weapon class. If I try:
Equipment::Weapon(std::string name, std::string something_else)
I just get member declaration not found,
Well, yes: your Weapon class is called Weapon, and it's constructor is called Weapon::Weapon. It inherits from Equipment, it isn't stored inside it.
and if I try:
Weapon::Weapon(std::string name, std::string something_else)
I get
no matching function for call to 'Equipments::Equipment::Equipment()'
That just means you left out the base class constructor (so it's trying to use the default constructor, which doesn't exist) - you don't show the whole code, but it ought to be
Weapon::Weapon(std::string name, std::string something_else)
: Equipment(name)
, something_else_(something_else)
{
}
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I'm making an IRC bot in C++, and it's supposed to react to messages in the IRC channel. The IRC channel belongs to a multiplayer game server, where people can communicate from in-game to IRC and vice versa. (Basically you can see things happening on the server, including chat, without actually having to be in the game).
I have a pretty basic class hierarchy set up to represent gradually more specific types of IRC events.
Represented on a graph it looks like this
Now, I'm trying to implement this kind of multi-level inheritance in C++, like this (please excuse my inconsistent styling. I also omitted CIrcCommand because it's very similar to CIngameCommand, and has the same problem)
CIrcEvent
class CIrcEvent
{
protected:
EventType m_type;
private:
void(*HandlerFunc)(std::string& nick, std::string& chan, std::vector<std::string>& message);
public:
CIrcEvent(void(*handler_ptr)(std::string& nick, std::string& chan, std::vector<std::string>& message))
{
m_type = EventType::IRCEVENT;
HandlerFunc = handler_ptr;
}
};
CCommand
class CCommand : public CIrcEvent
{
protected:
std::string m_name;
int m_level;
public:
std::string& GetCommandName() { return this->m_name; }
int GetLevel() { return this->m_level; }
};
CIngameCommand
class CIngameCommand : public CCommand
{
public:
CIngameCommand(std::string& CmdName, int CmdLevel, void(*CmdFuncPointer)(std::string& nick, std::vector<std::string>& message))
{
m_type = EventType::IGCMD;
m_name = CmdName;
m_level = CmdLevel;
HandlerFunc = CmdFuncPointer;
}
void Call(std::string& nick, std::vector<std::string>& message) { HandlerFunc(nick, message); }
private:
void(*HandlerFunc)(std::string& nick, std::vector<std::string>& message);
};
But inside of class CIngameCommand's constructor's definition...
CIngameCommand(std::string& CmdName, int CmdLevel, void(*CmdFuncPointer)(std::string& nick, std::vector<std::string>& message))
{ // <-- error on this line
m_type = EventType::IGCMD;
... I get a C2280 error.
IRCCommand.h(63): error C2280: 'CCommand::CCommand(void)': attempting to reference a deleted function
IRCCommand.h(43): note: compiler has generated 'CCommand::CCommand' here
I'm not sure what I'm doing wrong. I am aware that I haven't defined a custom constructor for CCommand class, but that's because as opposed to all the others, I'm only planning to use CCommand's derived classes (CIngameCommand and CIrcCommand), I'm never going to use it on its own.
What can I do to circumvent this error? Is my design flawed? If so, how can I implement this differently?
The deleted function is the default constructor of CIrcEvent. It has a user defined constructor and then that constructor has to be called from the derived class.
You don't define any constructors in CCommand so "compiler has generated 'CCommand::CCommand'". Such a compiler generated constructor will also call the base class' default constructor - but the base class doesn't have one!
So I'm getting this error message in QTcreator, undefined reference to table for waypointModel. This seems to be a common problem, and I've tried the solutions suggested on this site. Everything from running Qmake again, rebuilding, cleaning. Going through my makefile and program.pro file. Every header and source file is separated and on its right place in both the makefile and program.pro file. Yet I still get the error.
My waypointModel class isn't the only class being derived from the model Class, could this be the problem?
I also got the error on my destructor, but this disappeared when I initialised it in the header file as so:
virtual ~WaypointModel(){};
Why this suddenly work I don't know, any suggestions as to what may be the problem?
EDIT:
I want to add that the WaypointModel class is almost a direct copy of another class that was already existing, derived from the same class, but with another name, some extra data variables etc. This class does not experience the problems I'm facing now, even tho my class is almost a direct copy of it.
#Header # My derived class, and the source of the problem it seems.
Class WaypointModel: public Model
{
public:
// some functions
private:
//constructor / destructor
WaypointModel(const int unsigned int a_waypointNumber );
virtual ~WaypointModel();
//some data
unsigned int m_waypointNumber;
};
Header2 #
A general class, used as a baseclass for all other model classes.
class Model: public objectclass //
{
public:
//some functions
protected:
//construtor / destructor
Model(const std::string& a_classname);
virtual ~Model();
};
Source file WaypointModel
WaypointModel::WaypointModel(const unsigned int a_waypointNumber )
:Model("Modelname"), m_waypointNumber (a_waypointNumber)
{
// somefunc
}
WaypointModel::~WaypointModel()
{
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have some code which I could not compile it yet. The compiler says there should be a closing bracket, but I can't see a reason for this or place to put it. This is my code:
#include "Player.h"
Player(std::string val){
set_Name(val);
set_Alliance("NONE");
set_LastUpdate();
}
Player(std::string val, std::string ally){
set_Name(val);
set_Alliance(ally);
set_LastUpdate();
}
I have included in Player.h
This is the error:
error: expected ')' before 'val'
This is the prototype for constructor:
Player(std::string);
I am using GNU GCC compiler, under linux(ubuntu)
You are missing the class name from constructor outside the class definition. Try this:
Player::Player(std::string val){ // constructor outside class definition
set_Name(val);
set_Alliance("NONE");
set_LastUpdate();
}
Unverified speculation: With your current code, compiler sees Player(symbol1 symbol2) and takes that as creating object of class Player, and first thing it fails to understand is seeing two symbols as constructor argument, and gives a somewhat misleading error about that.
When you define methods, constructor, destructor etc. outside of the class, remember to tell the compiler that this belongs to the class using the class name following the scope operator :: and the name of the method, constructor, destructor etc with the matching parameters.
As a small example:
class Phone {
string number;
public:
string get_num();
void set_num(string const &num) { number = num; }
};
// Pay attention to this:
// we tell the compiler that get_num belongs to class Phone
string Phone::get_num()
{
return number;
}
int main()
{
Phone p;
p.set_num("123");
cout << p.get_num() << endl;
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I'm using the header file "sales.item"
I'm writing a little program and it is telling me that the header file, not my program, has an error. Somehow that last line isn't right. The error is saying that the string isbn is private.
#include <iostream>
#include "Sales_item.h"
int main()
{
Sales_item item1, item2;
std::cin >> item1 >> item2;
if (item1.isbn() == item2.isbn()) { // this checks if item1 and item2 are same book
In the Sales_item class you forgot to make the isbn method public, and left it at its default private visibility.
It should, in short, read something like this:
class Sales_item
{
public:
return_value isbn();
}
Without the public: line it will be private by default in C++ classes.
I'm going to go out on a limb and guess that your class is defined something like this:
class Sales_item
{
std::string isbn;
}
Classes and structs have public, private, and protected labels for their member data, and classes have their members labelled as private by default. You should change it to read:
class Sales_item
{
public:
std::string isbn;
}
EDIT:
When you add () (with or without parameters) to an identifier, you are telling the compiler to call it like a function. Take out the ()'s and your code should work.
I know it may seem like this has been asked before but I've looked around and the static method didn't work for me. Here's my code:
struct Customer {
public:
string get_name();
private:
string customer,first, last;
};
Here's where I call the function:
void creation::new_account() {
Customer::get_name(); //line it gives the error on.
}
Here's an example of some code that compiles fine.
struct Creation { public: string get_date(); private: string date; };
then I call it the same way
void Creation::new_account() { Creation::get_date();}
Hence my confusion why one works and the other doesn't.
EDIT: Ok I get it, I just realized I was calling a function of another struct inside a function definition that's part of a different class. I got it, thanks to all who answered
It is not declared static (needs to be static std::string get_name();). However, get_name() for Customer is a specific attribute of a Customer instance so having it static does not make sense, that is the same name for all instances of Customer. Declare an object of Customer and use it. It would make sense to have the name provided to the constructor of Customer, as surely a customer cannot exist without a name:
class Customer {
public:
Customer(std::string a_first_name,
std::string a_last_name) : first_name_(std::move(a_first_name)),
last_name_(std::move(a_last_name)) {}
std::string get_name();
private:
std::string first_name_;
std::string last_name_;
};
Declare an instance of Customer:
Customer c("stack", "overflow");
std::cout << c.get_name() << "\n";
Since your get_name is not declared static, it is a member function.
You probably need some constructors in your Customer class. Assuming you have some, you could code
Customer cust1("foo123","John","Doe");
string name1 = cust1.get_name();
You need an object (here cust1) to call its get_name member function (or method).
Time to spend many hours reading a good C++ programming book.
"The static method didn't work for me". It's not a method it's how the language works.
If you want to call some method without a concrete object, you need it to be static. Otherwise, you need an object.
Your code will work with one of the following :
struct Customer {
public:
static string get_name();
private:
string customer,first, last;
};
or
void creation::new_account() {
Customer c;
//stuff
c.get_name();
}