c++ - 派生类成员函数在 C++ 中失败

标签 c++

长话短说:

class A {
        public:
              A();
              ~A();
              void SetID(char* ID);
              char* GetID();
        protected:
              char ID[10];
};

class B: public A {
        public:
              B();
              ~B();
        protected:
              ...
        private:
              ...
};

然后在主要部分:

 ...
 B *temp = new B;
 temp->SetID("0x12345678");
 ...

然后编译器说“Expected constructor, destructor or type conversion before -> token” where "temp->SetID("0x12345678")"lies

有人给我一些提示吗??

Loki 建议的整个程序:

  #include <iostream>

  using namespace std;

  class A {
      public:
         A();
         ~A();
         void SetID(char* id);
         char* GetID();
       protected:
            char ID[10];
   };

   void A::SetID(char* id){
         strcpy(ID,id);          
    }
    char* A::GetID(){
         return ID;
    }
    class B: public A {
             public:
                 B();
                ~B();
             protected:
                int num;
     };


    int main(){
    B *temp = new B;
    B->SetID("0x12345678");
    cout<<B->GetID()<<endl;

    return 0;
    }

最佳答案

您正在使用 B,这是一种类型,您可能打算使用 temp,它是变量的名称。

代替:

int main(){
B *temp = new B;
B->SetID("0x12345678");
cout<<B->GetID()<<endl;

return 0;
}

你的意思可能是:

int main(){
B *temp = new B;
temp->SetID("0x12345678");
cout<<temp->GetID()<<endl;

return 0;
}

这更像是您发布的“摘录”。

关于c++ - 派生类成员函数在 C++ 中失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10865601/

相关文章:

c++ - 在 getline() 方面需要帮助

c++ - 如何覆盖 exit(),可能是通过抛出异常

c++ - c vs c++ on solaris 9 平台问题

c++ - Visual Studio 中的 Cuda 并行代码生成

c++ - 在 C++ 中使用 pthread_exit() 是否安全?

c++ - 命令行参数向后?

c++ - msgpack 打包结构

c++ - 在进行一些计数器操作时,我该如何做到,如果 setCounter() 没有参数,计数器将为零?

c++ - 使用 g++ 进行编译时 undefined reference

c++ - RAII 与异常