I'm taking a C++ Codecademy course and I ran into this error when calling my constructor.
profile.cpp:4:1: error: prototype for ‘Profile::Profile(std::__cxx11::string, int, std::__cxx11::string, std::__cxx11::string)’ does not match any in class ‘Profile’
Profile::Profile(string new_name, int new_age, string new_city, string new_pronouns) {
^~~~~~~
In file included from profile.cpp:1:0:
profile.hpp:4:7: error: candidates are: Profile::Profile(Profile&&)
class Profile {
^~~~~~~
profile.hpp:4:7: error: Profile::Profile(const Profile&)
profile.hpp:15:5: error: Profile::Profile(std::__cxx11::string, int, std::__cxx11::string, std::__cxx11::string, std::__cxx11::string)
Profile(string new_name, int new_age, string new_city, string new_country, string pronouns);
^~~~~~~
Here is my code in profile.cpp
#include "profile.hpp"
#include <vector>
using namespace std;
Profile::Profile(string new_name, int new_age, string new_city, string new_pronouns) {
name = new_name;
age = new_age;
city = new_city;
country = new_country;
pronouns = new_pronouns;
}
Here is my code in profile.hpp
#include <iostream>
#include <vector>
using namespace std;
class Profile {
private:
string name;
int age;
string city;
string country;
string pronouns;
vector<string> hobbies;
public:
Profile(string new_name, int new_age, string new_city, string new_country, string pronouns);
};
Here is my code in main.cpp
#include <iostream>
#include "profile.hpp"
int main() {
Profile sam("Sam Drakkila",30,"New York","USA","he/him");
}
I believe there is a problem with calling the constructor (as stated in title) but I am not sure. I have only started OOP a few days ago so I am very new and need a Simplified answer.
Here is the problem. I encountered an error before where I was calling 5 arguments in main.cpp but only declared 4 arguments to be called. To fix that issue I added "New_country" as a parameter. I simply forgot to update profile.cpp to add another parameter after updating profile.hpp with "new_country".
Related
I'm getting this error:
error: cannot convert 'std::string {aka std::basic_string<char>}' to 'char*' for argument '1' to 'char* strcpy(char*, const char*)'
Which I assume means it is unable to assign my one title string to my newtitle string, because they are not of the same type. (one being char and the other const char I suppose?)
strcpy(title, newtitle);
But they are both defined as type string, so I'm a little confused as to what it is that is giving me this error. Although I could be wrong about what this error means exactly.
#include<iostream>
#include<iomanip>
using namespace std;
#include <cstring>
class Movie{
private:
string title;
int year;
string director;
public:
void setTitle(string); // function prototype
void setYear(int); // function prototype
void setDirector(string); // function prototype
void displayMovie(); // function prototype
};
void Movie::setTitle(string newtitle)
{
strcpy(title, newtitle);
}
int main()
{
Movie myMovie;
string movietitle;
cout << "Enter the title of the Movie: " << endl;
cin >> movietitle;
myMovie.setTitle(movietitle);
}
std::strcpy expects its 1st argument to be char*, but std::string couldn't be implicitly converted to char*, that's why compiler complains.
You don't need to use strcpy for std::string, you could just
title = newtitle;
I have the following code:
//test.cpp
#include <Physical_file.h>
#include <boost\python.hpp>
using namespace boost::python;
using namespace FMS_Physical;
BOOST_PYTHON_MODULE(python_bridge)
{
class_<Physical_file, boost::noncopyable>("pf")
.def(init<const string&, optional<int, const string&>>())
;
}
//Physical_file.h
#include <fstream>
#include "tools.h"
using namespace std;
namespace FMS_Physical
{
class Physical_file
{
public:
fstream filefl;
string workingDir;
string fileName;
int fileSize;
block currBlock;
block FHBuffer;
bool opened;
string openMode;
/************
functions
************/
Physical_file(void);
Physical_file(const string &FileName, int FileSize,const string &Dir = getCurrentPath());
Physical_file(const string &FileName, const string &Type, const string &Dir = getCurrentPath());
~Physical_file(void);
};
}
there is some more code, I think it's irrelevant to the question.
when I try to compile the code, I get the following error:
Error 5 error C2664: 'FMS_Physical::Physical_file::Physical_file(const FMS_Physical::Physical_file &)' : cannot convert parameter 1 from 'const std::string' to 'const FMS_Physical::Physical_file &'
when I delete the optional from the definition of the constructor (in test.cpp) the error disappears, but I don't get the optional parameters.
I'm compiling using VS2010, python27 and c++ boost libs.
Can any one explain why I get this error and how can I solve it?
EDIT
I tried exposing the third constructor instead using the following line:
.def(init<const string&, const string &, optional<const string &>>())
this didn't cause any errors.
You have forgotten to give a default to FileSize.
For this declaration to work: init<const string&, optional<int, const string&>>
you need a constructor that can be called with only a const string& parameter. You do not have such at the moment, which is the exact complaint of the compiler.
I started learning C++, classes, objects, structures and more, but I'm having some problems with this.
#include <iostream>
using namespace std;
class Owner
{
public:
// Getters
string GetName(){return info.name;}
int GetAge(){return info.age;}
short int GetGender(){return info.gender;}
// Setters
void SetName(string value){info.name = value;}
void SetAge(int value){info.age = value;}
void SetGender(short int value){info.gender = value;}
private:
struct info
{
string name;
int age;
short int gender;
};
};
class Pet
{
public:
// Getters
string GetName(){return info.name;}
int GetAge(){return info.age;}
short int GetGender(){return info.gender;}
// Setters
void SetName(string value){info.name = value;}
void SetAge(int value){info.age = value;}
void SetGender(short int value){info.gender = value;}
private:
struct info
{
string name;
int age;
short int gender;
}
};
int main()
{
// Creating object ...
cout << "qq" << endl;
return 0;
}
But I get these errors when I try to compile it:
In member function 'std::string Owner::GetName()':|
main.cpp|9|error: expected primary-expression before '.' token|
In member function 'int Owner::GetAge()':|
main.cpp|10|error: expected primary-expression before '.' token|
In member function 'short int Owner::GetGender()':|
main.cpp|11|error: expected primary-expression before '.' token|
In member function 'void Owner::SetName(std::string)':|
main.cpp|14|error: expected unqualified-id before '.' token|
In member function 'void Owner::SetAge(int)':|
main.cpp|15|error: expected unqualified-id before '.' token|
In member function 'void Owner::SetGender(short int)':|
main.cpp|16|error: expected unqualified-id before '.' token|
main.cpp|45|error: expected unqualified-id before '}' token|
In member function 'std::string Pet::GetName()':|
main.cpp|30|error: expected primary-expression before '.' token|
In member function 'int Pet::GetAge()':|
main.cpp|31|error: expected primary-expression before '.' token|
In member function 'short int Pet::GetGender()':|
main.cpp|32|error: expected primary-expression before '.' token|
In member function 'void Pet::SetName(std::string)':|
main.cpp|35|error: expected unqualified-id before '.' token|
In member function 'void Pet::SetAge(int)':|
main.cpp|36|error: expected unqualified-id before '.' token|
In member function 'void Pet::SetGender(short int)':|
main.cpp|37|error: expected unqualified-id before '.' token|
||=== Build finished: 13 errors, 0 warnings ===|
Why does it give me so many errors?
I don't know why, because it is obvious that, for example,
string GetName()
{
return info.name;
}
returns a string, from the structure info.name
I'm using CodeBlocks.
You're declaring the struct as a type (Owner.info) instead of as a member (this->info). You probably want this:
struct OwnerInfo
{
string name;
int age;
short int gender;
};
class Owner {
// stuff..
private:
OwnerInfo info;
};
Or, the more reasonable version would be just having them there directly instead of inside a pointless struct:
class Owner {
// stuff..
private:
string name;
int age;
short int gender;
};
You're misunderstanding the syntax of the struct keyword, furthermore the actual member variable has to be declared before the member functions accessing it. So change your class declarations to something like
class Owner
{
private:
struct
{
string name;
int age;
short int gender;
} info;
public:
// Getters
string GetName(){return info.name;}
int GetAge(){return info.age;}
short int GetGender(){return info.gender;}
// Setters
void SetName(string value){info.name = value;}
void SetAge(int value){info.age = value;}
void SetGender(short int value){info.gender = value;}
};
The declaration:
private:
struct info
{
string name;
int age;
short int gender;
};
... defines the layout of a nested structure type info, much like the definition of the entire class does. However, info is now a type nested within Owner, not an instance of that type that is a member of Owner. Instead, try naming the struct Info, then declaring Info info = new Info(); in the private section of Owner.
Well you created a struct and therefor told your compiler "info" is a typ with the following attributes...
You need to declare a variable of the type "info".
info personalInfo;
Declare it as class member and you can create your Get-er and Set-er.
string GetName(){return personalInfo.name;}
More Information
You should create a object of struct info. You just cannot access a field of a struct directly without creating an object.. Why do you need the struct at all? try this
class Owner
{
private:
string name;
int age;
short int gender;
public:
// Getters
string GetName(){return this->name;}
int GetAge(){return this->age;}
short int GetGender(){return this->gender;}
// Setters
void SetName(string value){this->name = value;}
void SetAge(int value){this->age = value;}
void SetGender(short int value){this->gender = value;}
};
I try to make class where I can creat new object of Student.
I've got some problem with definition of class body (student.cpp) and class (student.h).
Error:
In file included from student.cpp:1:
student.h:21:7: warning: no newline at end of file
student.cpp:6: error: prototype for `Student::Student()' does not match any in class `Student'
student.h:6: error: candidates are: Student::Student(const Student&)
student.h:8: error: Student::Student(char*, char*, char*, char*, int, int, bool)
student.cpp
//body definition
#include "student.h"
#include <iostream>
Student::Student()
{
m_imie = "0";
m_nazwisko = "0";
m_pesel = "0";
m_indeks = "0";
m_wiek = 0;
m_semestr = 0;
m_plec = false;
}
student.h
//class definition without body
#include <string.h>
class Student {
//konstruktor domyslny
Student (char* imie, char* nazwisko, char* pesel, char* indeks, int wiek, int semestr, bool plec):
m_imie(imie), m_nazwisko(nazwisko), m_pesel(pesel), m_indeks(indeks), m_wiek(wiek), m_semestr(semestr), m_plec(plec)
{}
private:
char* m_imie;
char* m_nazwisko;
char* m_pesel;
char* m_indeks;
int m_wiek;
int m_semestr;
bool m_plec;
};
Your constructor in cpp file does not match constructor in header.
Every constructors/desctructors/methods realizations in cpp should be first defined in class in header.
If you want to have 2 constructors - 1 with no parameters and one with many parameters as you have. You need to add definition of your constructor in header.
//class definition without body
#include <string.h>
class Student {
//konstruktor domyslny
Student (char* imie, char* nazwisko, char* pesel, char* indeks, int wiek, int semestr, bool plec):
m_imie(imie), m_nazwisko(nazwisko), m_pesel(pesel), m_indeks(indeks), m_wiek(wiek), m_semestr(semestr), m_plec(plec)
{} //here really implementation made
Student(); //one more constructor without impementation
private:
char* m_imie;
char* m_nazwisko;
char* m_pesel;
char* m_indeks;
int m_wiek;
int m_semestr;
bool m_plec;
};
In you header file you declare that Student has just one constructor with all the written parameters but no default Student() constructor, you should add it to header:
class Student {
Student();
Student(char* imie, char* nazwisko ... ) {}
};
You wrote a body for a Student constructor that doesn't take any parameters:
Student::Student( /* NO PARAMETERS */ )
But this function, Student(), is not in the class-definition.
This generates the error:
prototype for `Student::Student()' does not match any in class `Student'
You need to write:
class Student {
public:
Student(); /* NOW it is declared as well as defined */
[... all the other stuff ...]
};
Now, there is both a prototype for Student() and also for Student(/* 7 parameters */)
The fix for the other error is simple:
student.h:21:7: warning: no newline at end of file
The fix is to put a newline at the end of the file! :-)
I have this in furniture.h:
#include <iostream>
#include <string>
using namespace std;
class Furniture {
public:
Furniture();
virtual ~Furniture();
void setname(string name);
void setprice(double price);
int getprice();
string getname();
private:
string name;
int price;
protected:
static int NumberOfItems;
int Id;
}
and this in furniture.cpp
#include "furniture.h"
void Furniture::setname(string name) {
this->name = name;
}
string Furniture::getname()
{
return this->name;
}
void Furniture::setprice(double price) {
this->price = price;
}
int Furniture::getprice() {
return this->price;
}
int main() {
Furniture *model = new Furniture();
model->setname("FinalDestiny");
model->setprice(149.99);
cout<<"Model name: "<<model->getname()<<" - price = "<<model->getprice();
}
But I get some errors like:
Error 1 error C2628: 'Furniture' followed by 'void' is illegal (did you forget a ';'?) c:\final\facultate\poo\laborator 1\furniture.cpp 3 1 POO_lab
Error 2 error C2556: 'Furniture Furniture::setname(std::string)' : overloaded function differs only by return type from 'void Furniture::setname(std::string)' c:\final\facultate\poo\laborator 1\furniture.cpp 3 1 POO_lab
Error 3 error C2371: 'Furniture::setname' : redefinition; different basic types c:\final\facultate\poo\laborator 1\furniture.cpp 3 1 POO_lab
Error 5 error C2264: 'Furniture::setname' : error in function definition or declaration; function not called c:\final\facultate\poo\laborator 1\furniture.cpp 19 1 POO_lab
What am I doing wrong?
You are missing a ; at the end of the class definition in your header file.
// ...snipped...
protected:
static int NumberOfItems;
int Id;
}; // <-- here
You've forgotten a semicolon at the end of your class definition.
// ...
protected:
static int NumberOfItems;
int Id;
}; // <--
I hate that about C++ :)
Two things;
You're not ending your class definition with a ;, you need one at the end of furniture.h.
You've declared that there's a constructor and destructor, but neither is implemented in your .cpp file.