C++ 对构造函数的混淆-预期为'';

C++ 对构造函数的混淆-预期为'';,c++,class,C++,Class,我没有将类与主函数放在同一个文件中,而是尝试使用一个#include。但是,当我这样做时,我的构造函数会出错。这是我的input.cpp文件: #ifndef input #define input using namespace std; #include <string> #include <iostream> class input { public: input(int sent) { s = sent; } v

我没有将类与主函数放在同一个文件中,而是尝试使用一个#include。但是,当我这样做时,我的构造函数会出错。这是我的input.cpp文件:

#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
#ifndef输入
#定义输入
使用名称空间std;
#包括
#包括
类输入
{
公众:
输入(已发送整数)
{
s=已发送;
}
无效读取();
无效存储(字符串s);
私人:
int-s;
};
#恩迪夫
这是我的主要职能:

#include <iostream>
#include <string>
using namespace std;
#include "input.cpp"



int main()
{

    cout<<"Hello, please enter your input"<<endl;
    string sent;
    getline(cin, sent);
    cout<<sent;

    input1 *newinput = new input1("hello");




    system("pause");

    return 0;
}
#包括
#包括
使用名称空间std;
#包括“input.cpp”
int main()
{
库特
  • 不要在标题中使用
    名称空间
  • 您有
    input
    作为宏常量,并且类的名称是相同的。恐怕这是您的问题的根源
  • 更喜欢使用构造函数初始化列表
    input(int-sent):s(sent){}
  • UPDT


    您可能需要构造函数能够接受字符串作为参数
    输入(const std::string&str1):str(str1){}
    ,其中
    str
    是处理字符串数据的类成员。

    您将构造函数定义为具有一个int类型的参数

    input(int sent)
    {
        s = sent;
    }
    
    但是试着将它作为参数传递称为字符串文本

    input *newinput = new input("hello");
    
    无法将类型为
    const char[6]
    的字符串文本隐式转换为int类型,并且该类没有其他接受字符数组作为参数的构造函数

    EDIT:
    您多次更改了原始帖子,因此现在不清楚是否在statent中使用name
    input1

    input1*newinput=newinput1(“你好”)

    是打字错误还是其他类型

    此外,还有一个与类名同名的宏定义

    #ifndef input
    #define input
    

    更改宏名称或类名。

    您定义类
    input
    ,但使用类
    input1
    。IntelliSense“错误”与编译没有任何关系。您不想包括.cpp文件,所以您知道。您应该只包括头文件,其中包含原型之类的内容(虽然您的cpp将包含实现)。为什么我不应该使用名称空间?抱歉,input1是一个输入错误,我已将其修复。我现在让程序运行!