C++ 未找到用户创建的类的构造函数

C++ 未找到用户创建的类的构造函数,c++,constructor,default-constructor,C++,Constructor,Default Constructor,这可能很难解释。下面是一段代码: void user_choice(string f) { bool goodchoice; string file = f; while (!goodchoice) { string answer; cin >> answer; if (answer == "save") { Data(file, "05/05/2014"); g

这可能很难解释。下面是一段代码:

void user_choice(string f) {
    bool goodchoice;
    string file = f;
    while (!goodchoice) {
        string answer;
        cin >> answer;

        if (answer == "save") {
            Data(file, "05/05/2014");
            goodchoice = true;
        }
        else if (answer == "look") {
            Data(file);
            goodchoice = true;
        }
        else cout << "I don't understand that. Please try again." << endl;
    }
}
我在主CPP文件中包含了Data.h头文件。“save”的if语句工作正常,但“look”的if语句告诉我,即使存在默认的数据构造函数,也没有默认的数据构造函数。我正在使用Visual Studio 2013

Data(file);
这不是调用使用字符串的构造函数。相反,它声明了一个名为
file
Data
对象,并使用默认构造函数来实现。这与:

Data file;
我无法100%地记得为什么会存在这种行为,但我相信它是从C继承来的。通过将它与最麻烦的解析相关联,您应该能够找到更多这样的例子,因为这种情况经常发生

有趣的是,这不是一种可以通过添加另一组括号来修复的情况。我不知道将此调用作为构造函数的公认方法(可能更好,如下所述),但强制转换是一种选择,调用函数是另一种选择,尽管两者看起来都不自然:

Data((file)); //doesn't work
Data(static_cast<string>(file)); //works
(void)Data(file); //works

template<typename T>
T &self(T &arg) {
    return arg;
}

Data(self(file)); //works
数据((文件))//不起作用
数据(静态_cast(文件))//作品
(作废)资料(档案)//作品
模板
T&self(T&arg){
返回arg;
}
数据(自我(文件))//作品

除非构造函数有副作用,否则它们不会做任何有意义的事情,只是创建和销毁一个对象。老实说,我不确定这些声明的意义是什么。如果构造函数负责执行一个操作,而您只需要临时对象,那么将
Data
改为函数。如果要使用实例调用其上的成员函数或类似函数,则应将实例存储在变量中,以便可以使用该变量执行它需要执行的操作。上述“修复”都应该是解决更深层问题的浅层解决方法。

数据是一个类,而不是一个对象。因此,
Data(file)
调用一个copy-construct函数。您不需要声明一个copy-construct函数。您需要声明一个对象,如
Data-Data
Data(file)
您需要两件事中的一件。。。这两个构造函数中的一个

Data();
//or
Data(string n = "default argument here");
当你说
Data(file)时,你也犯了一个错误因为您正在通过调用构造函数创建“Data”类型的临时对象,但没有存储它。我想你一定要这么做

void user_choice(string f) {
    bool goodchoice;
    Data myData;//this requires the constructors i listed above
    string file = f;
    while (!goodchoice) {
        string answer;
        cin >> answer;

        if (answer == "save") {
            //assign to data by calling the constructor
            myData=Data(file, "05/05/2014");
            goodchoice = true;
        }
        else if (answer == "look") {
            //assign to data by calling the constructor
            myData=Data(file);
            goodchoice = true;
        }
        else cout << "I don't understand that. Please try again." << endl;
    }
}
void user\u选项(字符串f){
布尔·古德乔伊;
Data myData;//这需要上面列出的构造函数
字符串文件=f;
而(!goodchoice){
字符串回答;
cin>>答案;
如果(回答=“保存”){
//通过调用构造函数为数据赋值
myData=数据(文件“2014年5月5日”);
好选择=正确;
}
否则,如果(回答==“看”){
//通过调用构造函数为数据赋值
myData=数据(文件);
好选择=正确;
}

否则您不能为
数据(字符串n)定义(提供一个主体)
作为旁白,您需要将
goodchoice
初始化为
false
,否则您是否会进入while循环是50/50。@TristanBrindle,50%输入,50%输出,50%鼻腔恶魔。编译器应该根据OP给我们的内容隐式生成副本构造函数。@chris但
文件
不是obje
数据的ct
,因此数据的默认复制构造函数不适用于
数据(文件)
。不适合,是的,但复制构造函数仍然存在。定义它使其适合将使其不再是复制构造函数。无论如何,前一个
文件
变量是什么类型实际上并不重要,因为此行创建了一个名为
文件
的新变量,该变量会隐藏旧变量。这不是固有错误创建临时对象而不存储它。例如,
std::ofstream(“某个文件”)@chris这正是我想表达的意思。如果我误解了他的问题,我道歉。很难说到底是什么问题,所以别担心。@chris我需要更多地了解这个程序,以便更好地猜测要做什么。看起来他好像在尝试使用数据()作为一个函数调用,但他说它是一个类,所以我不确定。由于正在使用构造函数,我假设其目的是创建一个数据对象。我认为一个设计问题是丢弃临时对象。至于构造函数错误,我不确定。
void user_choice(string f) {
    bool goodchoice;
    Data myData;//this requires the constructors i listed above
    string file = f;
    while (!goodchoice) {
        string answer;
        cin >> answer;

        if (answer == "save") {
            //assign to data by calling the constructor
            myData=Data(file, "05/05/2014");
            goodchoice = true;
        }
        else if (answer == "look") {
            //assign to data by calling the constructor
            myData=Data(file);
            goodchoice = true;
        }
        else cout << "I don't understand that. Please try again." << endl;
    }
}