Home > Net >  Error No instance of constructor matches the argument list
Error No instance of constructor matches the argument list

Time:09-13

I'm fairly new to C and I've been working on an assignment where I'm tasked with displaying given social media posts with classes and objects. I was doing okay but I keep getting this error that says No instance of constructor "Post::Post" matches argument list. I think I'm missing something simple and I was hoping someone could point out to me.`

Main.cpp

#include <iostream>
#include "Post.hpp"
#include <string>

int main() {

 
  Post post1("Chicken came before the egg confirmed!", "A new story just released where we finally get the answer of who came first."); //error appears on this line

  
  std::cout << post1.getTitle() << std::endl;
  std::cout << post1.getBody() << std::endl;
  post1.getTimeStamp();

 
  post1.setTitle("Actually the egg came first!");
  post1.setBody("Ok, maybe the decision is not final.");


  std::cout << std::endl;
  post1.displayPost();

Post.hpp

#ifndef POST_HPP
#define POST_HPP
#include <string>
#include <ctime>

class Post {
    private:
   std::string Title;
    std::string Body;
    time_t Time;
    
    public:
    void setTitle(string title); 
    string getTitle(); 

    void setBody(string body); 
    string getBody(); 
     
     void setTimeStamp();
    int getTimeStamp();
    
    void displayPost();

}


#endif

Post.cpp

#include "Post.hpp"
#include <string>


class Post {
    private:
    string Title;
    string Body;
    time_t Time;
    
    public:
    void setTitle(string title){
        Title = title;
    } 
    string getTitle(){
        return Title;
    }

    void setBody(string body){
        Body = body;
    } 
    string getBody(){
        return Body;
    } 
    void setTimeStamp(int time){
           Time = time;
    }
    void getTimeStamp(){
         struct tm *timestamp;
          time_t ltime;
           time(&ltime);
            timestamp = localtime(&ltime);
              printf("Today is %s",
             asctime(timestamp));

    }
    void displayPost(){
        cout << Title << Time << " :" << Body <<endl;
    }
      
    

};

CodePudding user response:

You need to add constructor to your code that takes two string parameters in your post.hpp file, something like:

public:
  Post(const std::string &s1, const std::string &s2);

CodePudding user response:

You forgot to code a custom constructor of your class Code. It goes like this:

class Post { 
   public: 

   Post(string title, string body) : Title(title), Body(body){} // It creates an instance of the class Post

   // Add member functions and variables below 
  •  Tags:  
  • c
  • Related