0

To make long story short:

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

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

Then in main:

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

Then the compiler said "Expected constructor,destructor or type conversion before -> token" where "temp->SetID("0x12345678")" lies

Anyone gimme some hints??

Whole Program as Loki suggested:

  #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;
    }
jasonkim
  • 696
  • 1
  • 7
  • 27

2 Answers2

1

You are using B, which is a type, where you probably meant to use temp which is the name of the variable.

Instead of:

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

return 0;
}

You probably meant:

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

return 0;
}

which is more like the "excerpt" that you posted.

CB Bailey
  • 755,051
  • 104
  • 632
  • 656
0

Let me guess.

You have closed main by an unpaired curly brace before you got to this code that you believe is in main. So it is outside of any function, and that is no place for expressions other than initializers.

Jirka Hanika
  • 13,301
  • 3
  • 46
  • 75