Question

I have a class myclass defined in a header file with a typedef in the private: section.

typedef int inttest;

My source file includes this header file, yet when attempting to use the typedef in the source file like so

inttest myclass::foo() { }

I get the error:

error: 'inttest' does not name a type

Why is this? Do I need to also declare the typedef in the source file?

Was it helpful?

Solution

First of all the typedef is defined in the scope of the class. So the compiler can not find the definition of the typedef if it is used as unqualified name as a return type. You could write for example

myclass::inttest myclass::foo() { }

However the compiler again will issue an error because the typedef is defined as private.

EDIT: I am sorry. The definition of the function I showed will be compiled.

However in the code that calls the function you will need to write either

myclass a;

int i = a.foo();

or

myclass a;

auto i = a.foo();

You may not write

myclass a;

myclass::inttest i = a.foo();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top