Home > database >  C Error when calling contructor (Codecademy)
C Error when calling contructor (Codecademy)

Time:12-16

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.

CodePudding user response:

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