I am pretty new to c++ and have no idea why I am getting this error, except that I think it's to do with using the string type for getter methods.
The error message:
C:\Users\Robin Douglas\Desktop\week6>g++ -c Student.cpp
Student.cpp:15:31: error: no 'std::string Student::get_name()' member function d
eclared in class 'Student'
Student.cpp:20:43: error: no 'std::string Student::get_degree_programme()' membe
r function declared in class 'Student'
Student.cpp:25:32: error: no 'std::string Student::get_level()' member function
declared in class 'Student'
Student.hpp
#include <string>
class Student
{
public:
Student(std::string, std::string, std::string);
std::string get_name;
std::string get_degree_programme;
std::string get_level;
private:
std::string name;
std::string degree_programme;
std::string level;
};
Student.cpp
#include <string>
#include "Student.hpp"
Student::Student(std::string n, std::string d, std::string l)
{
name = n;
degree_programme = d;
level = l;
}
std::string Student::get_name()
{
return name;
}
std::string Student::get_degree_programme()
{
return degree_programme;
}
std::string Student::get_level()
{
return level;
}
The following code defines fields (variables) rather then methods.
public:
Student(std::string, std::string, std::string);
std::string get_name;
std::string get_degree_programme;
std::string get_level;
Then, when you implement it in the .cpp file the compiler complains that you try to implement a method that was not declared (since you declared get_name to be a variable).
std::string Student::get_name()
{
return name;
}
To fix, just change your code as below:
public:
Student(std::string, std::string, std::string);
std::string get_name();
std::string get_degree_programme();
std::string get_level();
Related
I have a problem with c++ derived class constructor , I looked everywhere for similar problems but I dont seem to find any , I wish I will fin help in here.
basically I have a base classe with a constructor that has 4 parameters , and I derived a class from that base class but this time it has 6 parameters ,
both constructors are defined in a cpp file .
here are my files (in frensh):
**Entraineur.h**
class Entraineur: public Personne {
public:
Entraineur(std::string p_nom, std::string p_prenom, util::Date p_date, std::string p_num, std::string
m_numRAMQ, std::string m_sexe);
virtual ~Entraineur();
private:
std::string m_prenom;
std::string m_nom;
util::Date m_dateNaissance;
std::string m_telephone;
std::string m_numRAMQ;
std::string m_sexe;
**Entraineur.cpp**
#include "Personne.h"
#include "Entraineur.h"
#include <string>
#include "Date.h"
Entraineur::Entraineur(std::string p_nom, std::string p_prenom, util::Date p_date, std::string p_num,
std::string m_numRAMQ, std::string m_sexe) {
}
Entraineur::~Entraineur() {
// TODO Auto-generated destructor stub
}
**Personne.h**
class Personne {
public:
Personne(std::string p_nom, std::string p_prenom, util::Date p_date, std::string p_num);
virtual ~Personne();
private:
std::string m_prenom;
std::string m_nom;
util::Date m_dateNaissance;
std::string m_telephone;
std::string m_numRAMQ;
std::string m_sexe;
};
**Personne.cpp**
Personne::Personne(std::string p_nom, std::string p_prenom, util::Date p_date, std::string p_num) {
// TODO Auto-generated constructor stub
//setters
asgNom(p_nom);
asgPrenom(p_prenom);
asgDateNaissance(p_date);
asgTelephone(p_num);
}
Personne::~Personne(){
std::cout<< "detruit";
}
all the #includes are there.
please I beg for help.
I am having trouble getting my code to work, and not sure how to solve this problem.
#include <string>
struct car{
std::string car_name;
std::string get_name(void);
};
car::std::string get_name(){
return car_name;
}
Gives me error " error: 'std' in 'struct car' does not name a type car::std::string get_name(void)"
You should be declaring it as:
std::string car::get_name() {
return car_name;
}
This is because, get_name() is part of struct car. And std::string is a different entity from car. string is part of namespace std.
Newbie to C++ over here. I am trying to create 2 classes and interlink them using Composition, but I keep getting errors.
#include <iostream>
#include <string>
using namespace std;
class student
{
public: int roll_no;
string name;
string dob;
void student_display()
{
cout<<roll_no<< " "<<name<<" "<<dob<<endl;
}
student(int roll,string names,string dateofb)
{
roll_no=roll;
name=names;
dob=dateofb;
}
~student(){};
};
class course
{
public:string course_name;
int duration;
void course_display()
{
cout<<course_name<< " "<<duration<<" "<<endl;
s1.student_display()<<endl;
}
course (string c_name,int dur,student s2):course_name(c_name),duration(dur),s1(s2){}
~course(){};
private : student s1;
};
class college
{
public: string college_name;
string location;
course c1;
course (string col_name,string loc,course c2):college_name(col_name),location(loc),c1(c2){}
~course(){};
};
int main() {
student s5(001,"Noel","28/04/1994");
s5.student_display();
course c1("Engineering",4,s5(001,"Noel","28/04/1994"));
c1.course_display();
return 0;
}
The errors are as follows:
prog.cpp: In member function 'void course::course_display()':
prog.cpp:32:35: error: invalid operands of types 'void' and '<unresolved overloaded function type>' to binary 'operator<<'
s1.student_display()<<endl;
^
prog.cpp: At global scope:
prog.cpp:45:20: error: expected ')' before 'col_name'
course (string col_name,string loc,course c2):college_name(col_name),location(loc),c1(c2){}
^
prog.cpp:47:13: error: declaration of '~course' as member of 'college'
~course(){};
^
prog.cpp: In function 'int main()':
prog.cpp:52:54: error: no match for call to '(student) (int, const char [5], const char [11])'
course c1("Engineering",4,s5(001,"Noel","28/04/1994"));
Could someone please help me out? I have tried going through forums with the same error but cant figure it out
^
I have modified the code as follows:
#include <iostream>
#include <string>
using namespace std;
class student
{
public: int roll_no;
string name;
string dob;
void student_display()
{
cout<<roll_no<< " "<<name<<" "<<dob<<endl;
}
student(int roll,string names,string dateofb)
{
roll_no=roll;
name=names;
dob=dateofb;
}
~student(){};
};
class course
{
public:
string course_name;
int duration;
private:
student s1;
public:
void course_display()
{
cout<<course_name<< " "<<duration<<" "<<endl;
s1.student_display();
}
course (string c_name,int dur,student s2):course_name(c_name),duration(dur),s1(s2){}
~course(){};
//private: // shifted before course_display()
// student s1;
};
// Not used
//class college
//{
//public:
// string college_name;
// string location;
// course c1;
//
// course (string col_name,string loc,course c2):college_name(col_name),location(loc),c1(c2){}
//
// //~course(){}; // destructor mismatch
//};
int main() {
student s5(001,"Noel","28/04/1994");
s5.student_display();
//course c1("Engineering",4,s5(001,"Noel","28/04/1994"));
course c1("Engineering",4,s5);
c1.course_display();
return 0;
}
and the output is follows:
Line 30: Remove the semicolon.
Line 45: Looks like copy/paste error, should be the constructor of the college class, right? Then change the name from course to college. Same for the destructor on the following line.
Line 52: You create the object s5 2 lines above, I guess you want to pass this object to the constructor of the course object. If so then just remove the expression in the brackets after s5.
I get an error when try to call the function string SetToString(StringSet aSet); from an inherited class.
The header file for the base class:
#ifndef ITEM_H
#define ITEM_H
#include <ostream>
#include <set>
#include <string>
using namespace std;
typedef set<string> StringSet;
class Item
{
protected:
string title;
StringSet keywords;
public:
Item();
Item(const string& title, const string& keywords);
virtual ~Item();
void addKeywords(string keyword);
virtual ostream& print(ostream& out) const;
string getTitle() const;
string SetToString(StringSet aSet);
};
Implementation file for the base class :
#include "Item.h"
...
string Item::SetToString(StringSet aSet) {
string key;
int sizeCount = 0;
for (auto const& e : aSet) {
key += e;
sizeCount++;
if (sizeCount < aSet.size()) {
key += ", ";
}
}
SetToString(keywords);
return key;
}
...
When I try to do string k = SetToString(keywords); in the inherited class, I get the error : Error C2662 'std::string Item::SetToString(StringSet)': cannot convert 'this' pointer from 'const Book' to 'Item &'. How to fix this error, and why do I get it ?
Item::SetToString is not marked as const, so it cannot be called through a const pointer or reference, or on a const object.
You seem to be trying to call it from a function which is marked as const, and which therefore can't modify the current object (this) including by calling non-const functions on it.
Either make your inherited function not const, or make the base function const.
I'm having an error with my constructor in my classes.
In my .cpp file I've got:
Player::Player()
{
m_name="Jane";
m_amt=100;
}
and in my .h file I've got:
// Default constructor, does nothing.
Player();
// Creates a Player. Player name and amount.
Player(const char &name, int amt);
I'm getting the error:
error: invalid conversion from ‘const char*’ to ‘char’ [-fpermissive]
m_name="Jane";
So I tried converting it to a *char but that still doesn't work. Can someone help please?
You can either take a pointer:
Player(const char* name, int amt) { ... }
and you could use std::string to store the name and use char* just to construct this string:
private:
std::string m_name;
public:
Player(const char* name, int amt) : m_name(name) { }
or just use std::string everywhere and pass by reference to avoid redundant copies being created:
private:
std::string m_name;
public:
Player(const std::string& name, int amt) : m_name(name) { }
Note that the last version is quite flexible and it's possible to also pass const char* to it, example:
#include <iostream>
#include <string>
class Player {
private:
std::string m_name;
public:
Player(const std::string& name) : m_name(name){}
void printName() { std::cout << m_name; }
};
int main() {
Player p("Liho");
p.printName();
}
Show your full class/code. Following compiles fine for me.
cpp file...
#include"player.h"
Player::Player()
{
m_name="Jane";
m_amt=100;
}
.h file...
class Player {
public:
// Default constructor, does nothing.
Player();
// Creates a Player. Player name and amount.
Player(const char *name, int amt);
private:
char* m_name;
int m_amt;
};