简体   繁体   English

c++ 不存在从“myClass1”到“myClass1”的合适的用户定义转换

[英]c++ no suitable user-defined conversion from "myClass1" to "myClass1" exists

I'm very new to c++ and was really confused when this syntax error was highlighted by visual studio 2010. Definitions我对 C++ 非常陌生,当 Visual Studio 2010 突出显示此语法错误时,我真的很困惑。

class myClass1 {
    public: 
        myClass1();
}
class myClass2 {
    public:
        myClass2();
        void doSomething(myClass1 thing) {};
}

int main(int argc, char* argv[]) {
    vector<myClass1> arr;
    arr.resize(1);
    arr[0] = myClass1();
    myClass2 c2 = myClass2();
    c2.doSomething(arr[0]); //this line is being highlighted as giving the error in the title
};

I'm just really confused as to what this means.我真的很困惑这意味着什么。

The syntax error is at the line that i commented and it gives the error "no suitable user-defined conversion from "myClass1" to "myClass1".语法错误在我评论的那一行,它给出了错误“没有合适的用户定义的从“myClass1”到“myClass1”的转换。

Edit: sorry about not making the question clear编辑:抱歉没有把问题说清楚

The myClass* classes contain nothing/do nothing so it's kind of hard to understand your purpose. myClass* 类什么都不包含/什么都不做,所以很难理解你的目的。 Your code won't compile because the constructors are not defined.您的代码将无法编译,因为未定义构造函数。 You should always provide a minimum of relevant information (and not much more) in your code pastes.您应该始终在代码粘贴中提供最少的相关信息(而不是更多)。

To declare an instance of a myClass2 object on the stack, simply do:要在堆栈上声明 myClass2 对象的实例,只需执行以下操作:

myClass2 c2;

The doSomething method uses a copy of myClass1, which is probably not what you want. doSomething方法使用 myClass1 的副本,这可能不是您想要的。 The copy constructor is not defined (but there is nothing to copy).复制构造函数没有定义(但没有什么可复制的)。 Plus the function does nothing...另外这个函数什么都不做...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM