Hello All,

I need your help to understand , if the following code is ok ? (Baically to have polymorphism of different argument types )

class A{

...

...

public:

int function( structA *ptrA);
int function( structB *ptrB);

}

I am getting the compilation error saying the

syntax error at the 2nd line :

error C2061: syntax error : identifier 'structB'

Recommended Answers

All 5 Replies

First off thats not polymorphism. Second you need a forward decleration.

struct B;

struct A{
 A a;
 B b;
};

struct B{
 A a;
 B b;
};

Second, why are you using pointers? In C++ you use reference.

Thankou for the answer.
sorry, I meant function overloading.

Btw structA and structB are entirely different structures and both are defined in the header files included.

it must be a typo of some sort, because this code is fine and should compile. This will compile (I tested it):

#include <iostream>

struct structA {
  int val;
};

struct structB {
  double val;
};

class classA {
  public:
    int function(structA * ptrA) { std::cout << "function_A called!" << std::endl; return 0; };
    int function(structB * ptrB) { std::cout << "function_B called!" << std::endl; return 0; };
};

int main() {
  structA a; a.val = 3;
  structB b; b.val = 1.0;
  classA obj;
  obj.function(&a);
  obj.function(&b);
};

Sorry for the delay, Thanks a lot Mike for the info.

I am making changes to already existing large code base, may be there is some issue somewhere , which is bit difficult to figure out..

Thankyou, problem is solved, as it was a huge code, and this new structure (structB which i added ) required forward declarations in some files ...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.