c++ - Confusion about constructors - expected a ';' -


instead of putting class in same file main function, i'm trying use #include. though, when this, error constructor. input.cpp file:

#ifndef input #define input using namespace std; #include <string> #include <iostream>  class input { public:     input(int sent)     {         s = sent;     }      void read();     void store(string s);  private:      int s;  }; #endif 

this main function:

#include <iostream> #include <string> using namespace std; #include "input.cpp"    int main() {      cout<<"hello, please enter input"<<endl;     string sent;     getline(cin, sent);     cout<<sent;      input1 *newinput = new input1("hello");         system("pause");      return 0; } 

the error i'm getting

"intellisense expected ';'"

in body of constructor. though, when copy / paste class directly main.cpp file, error goes away. idea on causing this?

  1. do no use using namespace in headers
  2. you have input macro constant , name of class same. afraid it's root problem.
  3. prefer use constructor initialization lists input(int sent) : s(sent) {}

updt

you may need constructor able accept string parameter input(const std::string& str1) : str(str1) {} str class member handle string data.


Comments