No declaration matches in Codelite IDE - c++

I have been looking in different threads with this error which is quite common but it feels like the IDE I am using messed with my workspace and I can't quite find the problem. I am setting up an extremely basic class called "Movie" that is specified below:
Movie.hpp :
#ifndef MOVIE_HPP
#define MOVIE_HPP
#include <iostream>
#include <string>
using std::string, std::cout,std::size_t;
class Movie
{
private:
std::string name;
std::string rating;
int watched_ctr;
public:
Movie(const string& name, const string& rating, int watched_ctr);
~Movie();
//getters
string get_name() const;
string get_rating() const;
int get_watched() const;
//setters
void set_name(string name);
void set_rating(string rating);
void set_watched(int watched_ctr);
};
#endif // MOVIE_HPP
Movie.cpp:
#include <iostream>
#include <string>
#include "Movie.hpp"
using std::string, std::cout,std::size_t,std::endl;
Movie::Movie(const string& name, const string& rating, int watched_ctr)
: name(name) , rating(rating) , watched_ctr(watched_ctr) {
}
Movie::~Movie()
{
cout << "Destructor for Movies class called /n";
}
//Getters
string Movie::get_name(){return name;}
string Movie::get_rating(){return rating;}
string Movie::get_watched(){return watched_ctr;}
//Setters
void Movie::set_name(std::string n){this -> name = n;}
void Movie::set_rating(std::string rating){this -> rating = rating;}
void Movie::set_watched(int ctr){this -> watched_ctr = ctr;}
The main.cpp I am trying only consists in creating one Movie object:
#include <iostream>
#include <string>
#include "Movie.hpp"
using std::string, std::cout,std::size_t,std::endl;
int main()
{
Movie StarTrek("Star Trek", "G", 20);
}
As you can see, I set all the attribute to private in order to exercise with the set/get methods but I keep stumbling upon the same error on each of them stating >"C:/Users/.../ProjectsAndTests/MoviesClass/Movie.cpp:18:8: error: no declaration matches 'std::__cxx11::string Movie::get_name()"
if you could give me a hint on what might cause this error I would greatly appreciate thank you!
I tried opening another workspace with classes implemented inside of them and the syntax I am using is very close from this test workspace I opened which compiled fine (no error regarding declaration match).

There are 2 problems with your code.
First while defining the member functions outside class you're not using the const. So to solve this problem we must use const when defining the member function outside the class.
Second, the member function Movie::get_watched() is declared with the return type of string but while defining that member function you're using the return type int. To solve this, change the return type while defining the member function to match the return type in the declaration.
//----------------------vvvvv--------->added const
string Movie::get_name()const
{
return name;
}
string Movie::get_rating()const
{
return rating;
}
vvv------------------------------>changed return type to int
int Movie::get_watched()const
{
return watched_ctr;
}
Working demo

Related

Compiler does't recognize overloaded method in another method

I have two classes: "Station" which has method getName() returning string and "Profit" which has the overloaded method sellAt(string stName), sellAt(Station st). To avoid duplicate code I call sellAt(string stName) in sellAt(string stName), however in some cases (see code example below) compiler gives an error: "no instance of overloaded function "Profit::SellAt" matches the argument list. Argument types are: (std::string)". Is it a bug or I miss something?
Station.h
#pragma once
#include <string>
using namespace std;
class Station
{
private:
string sName;
public:
Station(string name);
string getName();
};
Station.cpp
#include "Station.h"
Station::Station(string name)
:sName(name)
{}
string Station::getName()
{
return sName;
}
Profit.h
#pragma once
#include "Station.h"
#include <string>
class Profit
{
public:
double SellAt(string& stName);
double SellAt(Station& st);
};
Profit.cpp
#include "Profit.h"
double Profit::SellAt(const string& stName)
{
// do stuff
}
// Works as expected
double Profit::SellAt(Station& st)
{
string stName = st.getName();
return SellAt(stName);
}
// Compile error
double Profit::SellAt(Station& st)
{
return SellAt(st.getName());
}
// Compile error
double Profit::SellAt(Station& st)
{
double result = SellAt(st.getName());
return result;
}
Yksisarvinen answered in the comment below the original question:
st.getName() is a temporary. You cannot bind non-const reference to a temporary. I suppose you shouldn't want to modify stName in Profit::SellAt(), so change the type of argument to const std::string&.
Thanks for the help!

Compiler error in C++

Here is the error I am getting.
CruiseShip.h:10: error: expected ‘;’ before ‘::’ token
CruiseShip.cpp:8: error: expected ‘)’ before ‘name’ make: ***
[CruiseShip.o] Error 1
CruiseShip.h
CruiseShip(std::string name,std::string year, int maxPassengers)::Ship(std::string name,std::string year);
CruiseShip.cpp
CruiseShip(string name, string year, int maxPassengers)::Ship(string name, string year){
maxPassengers=0;
}
These are the line's where the error occurs.
Here is the rest of the code:
CruiseShip.cpp
#include <iostream>
#include "Ship.h"
#include "CruiseShip.h"
using namespace std;
CruiseShip(string name, string year, int maxPassengers)::Ship(string name, string year){
maxPassengers=0;
}
void CruiseShip::setPass(int maxPassengers){
this->maxPassengers=maxPassengers;
}
int CruiseShip::getPass(){
return maxPassengers;
}
void CruiseShip::print(){
cout<<"The name of the ship is "<<getName()<<endl;
cout<<"The capacity of the ship is "<<maxPassengers<<endl;
}
CruiseShip.h
#ifndef CRUISESHIP_H_
#define CRUISESHIP_H_
#include <string>
class CruiseShip: public Ship{
protected:
int maxPassengers;
public:
CruiseShip(std::string name,std::string year, int maxPassengers)::Ship(std::string name,std::string year);
void setPass(int);
int getPass();
virtual void print();
};
#endif
Apparently, CruiseShip inherits from Ship.
The declaration should say only what the constructor's prototype is,
CruiseShip(std::string name, std::string year, int maxPassengers);
and the definition does the initialisation:
CruiseShip::CruiseShip(string name, string year, int maxPassengers)
: Ship(name, year),
maxPassengers(maxPassengers)
{
}
Note that there's only a single colon and that the base class initialization doesn't mention the types, just like a function call.
Also, the constructor definition needs the scope specification CruiseShip::.
This line doesn't seem to make any sense.
What do you think it is supposed to do?
CruiseShip(std::string name,std::string year, int maxPassengers)::Ship(std::string name,std::string year);
It looks like the start of a constructor for class CruiseShip, but then has a scoping (::) before starting to look like the constructor for class Ship.
Here is what I think you mean:
In Header(.h) file:
#pragma once
#include <string>
using std::string;
class CruiseShip :
public Ship // Class inherits from base-class Ship
{
// Constructor takes 3 parameters:
CruiseShip(const string& name, const string& year, int maxPassengers);
};
In Implementation(.cpp) File:
// Implementation of the Constructor, which begins by passing
// name and year to the Base-Class constructor.
// Then completes the constructor by handling the maxPassengers parameter.
CruiseShip::CruiseShip(const string& name, const string& year, int maxPassengers):
Ship(name, year) // Call the base-class constructor
{
this->maxPassengers = maxPassengers; // Also assign member variable.
}
A few other notes:
You should generally pass variables by const-reference if you don't have a good reason for passing by value. This will avoid needless copy-constructors.
Avoid the whole #ifdef - #endif protection by using #pragma once, which is supported by most major compilers now.
Don't do using namespace std;. It brings in the entire namespace, which is really big. Just import what you need: using std::string; (see This Topic)
Your Ship class has to have something like this :
Ship(std::string,std::string);
in public declarations. Because this is what you are calling when you give parameters in CruiseShip
The way you make a correct constructor with inherence is this way :
CruiseShip::CruiseShip(string name, string year, int maxPassengers):Ship(name,year){
maxPassengers=0;
}
You are calling the constructor with parameters Ship(std::string,std::string) that takes parameters given by CruiseShip. And you simply tell the program which variables you are giving
Your CruiseShip class because it's wrong. You don't tell the program to call Ship first
#ifndef CRUISESHIP_H_
#define CRUISESHIP_H_
#include <string>
class CruiseShip: public Ship{
protected:
int maxPassengers;
public:
CruiseShip(std::string name,std::string year, int maxPassengers);
void setPass(int);
int getPass();
virtual void print();
};
#endif

What is wrong? - Composition compile error (with family+person classes example)

I got two classes, one named Person that I checked is working (I can create objects of that class so the problem should not be here).
I then have another class called Family with composition from Person:
Family.h
#include "Person.h"
class Family
{
public:
Family();
void printFamily();
private:
Person dad_();
Person mum_();
Person son_();
Person daughter_();
};
Family.cpp
#include "Family.h"
Family::Family()
{
}
void printFamily()
{
dad_.printAll();
mum_.printAll();
son_.printAll();
daughter_.printAll();
//printAll() is a function in the Person class that worked when
//I tested it earlier with only the person class
}
But when i try to compile this I get an error:
left of '.printAll' must have class/struct/union
'son_' : undeclared identifier
This error goes for all the .printAll() calls in family.cpp.
I can't see why this wouldn't work, so I hope you can.
Edit1:
Ok i changed
void printFamily()
to
void Family::printFamily()
That removes one error, but i still get
left of '.printAll' must have class/struct/union
Edit2
Ah my bad with the Person calls i changed them to
Person dad_;
and the same with the rest.
Seems like their might be an error with my Person class so i will post that also
Person.h
#include <string>
using namespace std;
class Person
{
public:
Person( const string & = "000000-0000", const string & = "N", const string & = "",const string & = "N");
~Person();
void setFirstName(const string &);
void setMiddleName(const string &);
void setLastName(const string &);
void getData(string &,string &,string &,string &);
static int getNumberOfPersons();
void printPartially() const;
void printAll() const;
bool checkForSameName(const Person &);
private:
string firstName_;
string middleName_;
string lastName_;
string socialSecNumber_;
static int numberOfPersons_;
};
Person.cpp
#include "Person.h"
#include <iostream>
int Person::numberOfPersons_ = 0;
Person::Person( const string &sNumber, const string &firstName, const string &middleName,const string &lastName )
:firstName_(firstName),middleName_(middleName),lastName_(lastName),socialSecNumber_(sNumber)
{
numberOfPersons_ ++;
}
Person::~Person()
{
numberOfPersons_--;
}
void Person::setFirstName(const string &firstName)
{ firstName_ = firstName; }
void Person::setMiddleName(const string &middleName)
{ middleName_ = middleName; }
void Person::setLastName(const string &lastName)
{lastName_ = lastName;}
void Person::getData(string &fName,string &mName,string &lName,string &sNumber)
{
fName = firstName_;
mName = middleName_;
lName = lastName_;
sNumber = socialSecNumber_;
}
int Person::getNumberOfPersons()
{
return numberOfPersons_;
}
void Person::printPartially() const
{
cout <<"Navn: "<<firstName_<<" "<<middleName_<<" "<<lastName_<<endl;
cout <<"Født: ";
for (int i = 0;i<6;i++)
{
cout <<socialSecNumber_.at(i);
}
}
void Person::printAll() const
{
cout <<"Navn: "<<firstName_<<" "<<middleName_<<" "<<lastName_<<endl;
cout <<"Personnr: "<<socialSecNumber_<<endl;
}
bool Person::checkForSameName(const Person &p)
{
if (p.firstName_ == firstName_ && p.middleName_ ==middleName_ && p.lastName_ == lastName_)
return true;
else
return false;
}
Now i am getting some new errors:
error C2011: 'Person' : 'class' type redefinition
see declaration of 'Person'
'Family::dad_' uses undefined class 'Person'
The "dad" error applies to the whole family
You have a few syntax issues.
First, you're declaring each of what are supposed to be member variables as functions which return Person. They should look like (note, no parens):
Person dad_;
Person mum_;
Person son_;
Person daughter_;
You're also missing the scoping on your definition of printFamily:
void Family::printFamily() {
...
}
Without the preceding Family::, C++ thinks you're defining a free function, and doesn't know to look inside the Family class for the declarations of dad_, mum_, etc.
Additionally, at least with the code you've shown, there's no way to initialize the people in your class. The Family constructor should take arguments to define the people, or you should have setters which allow defining them later. Right now, you'll get 4 identical people, set up however the default person constructor builds them.
I would normally prefer the constructor method, but I have other design reservations about your code to begin with (e.g. Does a family always contain mum, dad, brother, sister?) and that's not really what this question is about.
The line:
Person dad_();
says that dad_ is a function that returns a Person, not an object. Did you mean that? Similarly for others.
Try
Person dad_;
Family.h
#include "Person.h"
class Family
{
public:
Family();
void printFamily();
private:
Person dad_;
Person mum_;
Person son_;
Person daughter_;
};
Family.cpp
#include "Family.h"
Family::Family()
{
}
void Family::printFamily()
{
dad_.printAll();
mum_.printAll();
son_.printAll();
daughter_.printAll();
//printAll() is a function in the Person class that worked when
//I tested it earlier with only the person class
}
The out of line definition of a member function needs to include the class name:
void Family::printFamily()
{
//...
Surprisingly, you already got this right for the constructor but then immediately forgot...
Second, your private class members are functions, not data members (which is odd), but if that's deliberate, you need to call them:
dad_().printAll();
// ^^^

No matching constructor for initialization of

I’ve seen similar questions on StackOverflow, but none of them seems to apply to me.
Here is my code:
Option.cpp
#include "Option.h"
Option::Option(string valueName, string description, OptionType type){
this->valueName = valueName;
this->description = description;
this->type = type;
};
Option.h
#include <iostream>
using namespace std;
enum OptionType { FLAG, REQUIRED, NORMAL };
class Option {
string valueName, description, value;
OptionType type;
public:
Option(string valueName, string description, OptionType type);
void setValue(string value) {
this->value = value;
};
string getValueName() {
return this->valueName;
};
string getDescription() {
return this->description;
};
OptionType getType() {
return this->type;
};
};
Options.cpp
#include "Options.h"
using namespace std;
Options::Options(int _argc, const char * _argv[]) : argv(_argv) {
this->argc = _argc;
}
Options::~Options() {
options.~unordered_map();
}
void Options::printHelp() {
for (auto &i : options) {
cout << i.first << '\t' << i.second.getDescription() << '\n';
}
}
void Options::addFlag(string flagName, string description) {
}
void Options::addOption(string optionName, string valueName, string description, OptionType type) {
Option option(valueName, description, type);
options[optionName]=option;
}
void Options::addOptionAlias(string aliasName, string optionName) {
}
Options.h
#include <iostream>
#include <unordered_map>
#include "Option.h"
using namespace std;
class Options {
unordered_map<string, Option> options;
int argc;
const char ** argv;
public:
Options(int argc, const char * argv[]);
~Options();
void parse();
void addOption(string optionName, string valueName, string description, OptionType type);
void addFlag(string flagName, string description);
void addOptionAlias(string aliasName, string optionName);
void getOption(string optionName);
void printHelp();
};
It's in options.cpp on the line Option option(valueName, description, type); that the error seems to stem from, but for the life of me, I can’t figure out why. As far as I can see, the constructor in Option takes the right types.
The problem is actually in the next line:
options[optionName]=option;
That first calls the operator[] in the map, that searchs for the given key and returns the associated value. If the key is not found, it insert a default initialized value connected to that key. Then this value is copy assigned with your option.
Do you see the problem? Your Option class does not have a default constructor, so it cannot be default initialized! Read carefully your error message, surely it is talking about the default constructor, not the one you are looking at.
You have several solutions. The easiest would be to write a default constructor for your class.
The alternative would be never to use operator[] in the map so that the default constructor is never needed. If that's what you want to do, to insert an item you write:
options.insert(std::make_pair(optionName, option));
Finally, if you are using C++11 (or later) and a compliant enough compiler, you can even build the object directly into the container: zero copy overhead and you don't even need the copy constructor!
options.emplace(std::piecewise_construct,
std::forward_as_tuple(optionName),
std::forward_as_tuple(valueName, description, type));
There's a mismatch between the declaration of the constructor in the header and the definition in the source file.
In header...
Option(string& valueName, string& description, OptionType& type);
In source file...
Option::Option(string valueName, string description, OptionType type){
Notice the parameters are defined as references (e.g., string&) in the header, but as objects (e.g., string) in the source.

How to insert class into stl map on class initialization

SOLVED: http://pastebin.com/seEaALZh
I was trying to create simple items system, where i can get item information by its id. I cant use array, because items ids are lets say random. I want to use declared items as variables and i want to quickly find any item info by its id. The only way i found is stl map.
So I have this simple code:
main.h
#include <iostream>
#include <map>
enum
{
weapon,
ammo
};
class c_items
{
bool operator()(const c_items& l, const c_items& r) const
{
return (l.id < r.id);
}
public:
c_items(void){};
c_items(int id, char name[], int type);
char *name;
int type;
int id;
};
extern std::map<int,c_items> Stuff;
c_items::c_items(int id, char name[], int type) : id(id), type(type), name(name)
{
Stuff[id] = c_items(id, name, type);
}
const c_items
brass_knuckles (546, "Brass knuckles", weapon),
golf_club (2165, "Gold club", weapon);
main.cpp
#include "main.h"
std::map<int,c_items> Stuff;
using namespace std;
int main()
{
// cout << Stuff[2165].name.data();
return 1;
}
And for some reason program crashes. How to correctly insert class data into map on class initialization?
The problem is order of initialization. The static constructors for brass_knuckles and golf_club run first, before the static constructor for Stuff, so they attempt to insert into a map that is not yet constructed.
In addition, you NEVER want variable DEFINITIONS in a header file, since if you include the header file in multiple source files, you end up with multiple definitions, which will at best cause a link failure. So you should move the DEFINITIONS out of the .h file and into the .cpp file. Putting them AFTER the definition of Stuff will fix the order of initialization problem.
You can have a DECLARATION of the variables in the header file if you want to use them in other compilation units:
extern const c_items brass_knuckles, golf_club;
you cannot put c_item in Stuff like that
instead
std::map<int,c_items> Stuff = { {item1.id, item1}, {item2.id, item2}};
but you also need to all the recommendation made by #Chris Dodd
so
c_items::c_items(int id, char name[], int type) : id(id), type(type), name(name)
{}
extern const c_items brass_knuckles, golf_club;
and in the main.cpp
const c_items brass_knuckles = {546, "Brass knuckles", weapon);
const c_items golf_club = (2165, "Gold club", weapon);
std::map<int,c_items> Stuff = { {brass_knuckles.id, brass_knuckles}, etc....};