c++ 为什么我的编译器一直显示问题表达式必须是可修改的左值[重复]

toiithl6  于 2023-05-08  发布在  其他
关注(0)|答案(2)|浏览(181)

此问题已在此处有答案

Why can't member initializers use parentheses?(2个答案)
How to create an object in a form like this: ifstream in();(1个答案)
Trying to understand default constructors and member initialisatioon(3个答案)
A confusing detail about the Most Vexing Parse(4个答案)
6天前关闭。

#ifndef constructors_h_
    #define constructors_h_
    #include<iostream>

    using namespace std;
    class person{
        private:
        int age();
        string name();
        public:
        person();
        person(string newName){
            name=newName; age=0;};
            string tostring();
    

    };
    #endif constructors_h_

我是一个完全新的编码是试图使用使用字符串流,但我的编译器不断显示这个问题(与代码)

rqdpfwrv

rqdpfwrv1#

您可能希望agename是变量而不是函数,并使用()初始化它们,但这使它们成为函数声明。如果要初始化它们,可以删除(),或者使用{}
你可能想在构造函数中使用一个const std::string&,而不仅仅是一个std::string,以避免复制整个字符串。
请永远不要在头文件中使用using namespace。你不应该在C文件中这样做,但永远不要在头文件中这样做,因为头文件只是被编译器复制到你的C文件中,包括头文件,因此你在每个C++文件中都有using,这可能会导致与其他函数冲突。有关更多信息,请参阅using namespace std上的这篇文章。

w80xi6nr

w80xi6nr2#

您将字符串对象newName赋给函数string name(),将整数0赋给函数int age(),这显然是不允许的,可能也不是您想要做的。

相关问题