Error 'object instance' does not name a type

Go To Last Post
12 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello everybody!

OK, I got stuck with this stupid error, and I need someone's fresh glance at my code!

 

I am trying to compile my c++ program in Atmel Studio 7 which uses my own EncDRV class. Here is the short code.

 

EncDRV.h

#ifndef __ENCDRV_H__
#define __ENCDRV_H__

class EncDRV
{
public:
	EncDRV();
	static void (*callback)();
}; //EncDRV

#endif //__ENCDRV_H__

 

EncDRV.cpp

#include <EncDRV.h>

// default constructor
EncDRV::EncDRV(void)
{

} //EncDRV

 

main.cpp

#include <EncDRV.h>

EncDRV encoder;
encoder.callback = encHandle;   //               <-- here I get 'encoder' does not name a type error

void encHandle(void)
{

}

 

The error occurs in the main.cpp file at the line "encoder.callback = encHandle;".

 

Nothing depends on the variable I am trying to reach in the instance, the problem stays the same. Please, help!

This topic has a solution.

Just to let you know...

Last Edited: Tue. Nov 24, 2020 - 03:29 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

You can't access a static member variable through an instance of a class. Use:

A static member variable can't be accessed through an instance of a class. Use:

EncDRV::callback = encHandle;

Some more issues:

  • encHandle is being used before it has been defined. Either provide a prototype or move the assignment to after the definition of encHandle.
  • callback has been declared in the EncDRV class body, but it has not been defined in the source file. With C++17 or later static member variables can be declared inline to avoid having to define them in a source file.
void ( * EncDRV::callback )() = encHandle; // in source file if pre C++17 or not using inline

inline static void ( * callback )() = encHandle; // in class body if C++17 or later

 

github.com/apcountryman/build-avr-gcc: a script for building avr-gcc

github.com/apcountryman/toolchain-avr-gcc: a CMake toolchain for cross compiling for the Atmel AVR family of microcontrollers

Last Edited: Tue. Nov 24, 2020 - 06:19 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You can't access a static member variable through an instance of a class. Use:

Well, in Arduino IDE it worked somehow. Thanks for the hint! Corrected.

 

  • callback has been declared in the EncDRV class body, but it has not been defined in the source file. With C++17 or later static member variables can be declared inline to avoid having to define them in a source file.

OK, I added it to the EncDRV.cpp file and now it's the same error but for the callback: 'callback' in 'class EncDRV' does not name a type

 

P.S. And more to say: I can't access even nonstatic members of other classes from the file scope of main.cpp with the same error.

Just to let you know...

Last Edited: Tue. Nov 24, 2020 - 07:23 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

The below assumes this code is pre C++11. Could be simplified and improved for C++11 and later.

 

Header:

#ifndef __ENCDRV_H__
#define __ENCDRV_H__

class EncDRV {
    public:
        typedef void ( * Callback )();

        EncDRV()
        {
        }

        static Callback callback;
};

void encHandle();

#endif // __ENCDRV_H__

 

Source:

#include <cstddef>

#include "EncDRV.h"

EncDRV::Callback EncDRV::callback = NULL;

void encHandle()
{
}

 

Main:

#include "EncDRV.h"

int main()
{
}

The above compiles just fine. However, I'm guessing that you aren't actually trying to initialize callback to encHandle, but rather assign to it later on:

 

Header:

#ifndef __ENCDRV_H__
#define __ENCDRV_H__

class EncDRV {
    public:
        typedef void ( * Callback )();

        EncDRV()
        {
        }

        static Callback callback;
};

#endif // __ENCDRV_H__

 

Source

#include <cstddef>

#include "EncDRV.h"

EncDRV::Callback EncDRV::callback = NULL;

 

Main:

#include "EncDRV.h"

void encHandle()
{
}

int main()
{
    EncDRV::callback = encHandle;
}

 

Another note: leading underscores shouldn't be used in symbol names since names beginning with leading underscores are reserved for the compiler's and standard library's use.

github.com/apcountryman/build-avr-gcc: a script for building avr-gcc

github.com/apcountryman/toolchain-avr-gcc: a CMake toolchain for cross compiling for the Atmel AVR family of microcontrollers

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

apcountryman wrote:
Another note: leading underscores shouldn't be used in symbol names since names beginning with leading underscores are reserved for the compiler's and standard library's use.

 

Isn't that only for C? C++ copes with namespaces much better - probably because it actually implements them.

 

There seems to be two common ways to identify member variables in C++. either _var or mVar

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

OK, I solved it for now.

 

The only thing that helped me was your hint that encHandle has to be declared before I pass it into callback. In fact

encoder.callback = encHandle;

doesn't work only in the file scope for some reason. So I moved it to main and it all works fine, and the class static members are all accessible via encoder instance. No typedef and null pointers magic required here.

 

Thanks a lot! :-)

Just to let you know...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

main.cpp

#include <EncDRV.h>

EncDRV encoder;
encoder.callback = encHandle;   //               <-- here I get 'encoder' does not name a type error

void encHandle(void)
{

}

The indicated line of code does not exist within a function. Of course you'll get an error.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Sorry, I don't understand within which particular function it doesn't exist. It exists in the file scope and supposed to serve as a global variable so the interrupt service routines could call encHandle when required.

Just to let you know...

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

C only allows '=' outside of a function body when specifying initialisers during object instantiation. You can't assign a value to an existing object (a run time action) outside a function. Surely what you want is something more like:

class EncDRV {
    public:
        typedef void ( * Callback )();

        EncDRV(Callback initCallback = NULL) :
           callback(initCallback)
        {
        }

        static Callback callback;
};

then at the point of instantiation:

EncDRV encoder(encHandle);

int main(void) {
    yada yada yada
}

This will make the assignment in the c'tor.

 

As I made it a default parameter then:

EncDRV encoder();

int main(void) {
    yada yada yada
}

is also possible - in which case it will be set to NULL by default.

Last Edited: Tue. Nov 24, 2020 - 09:19 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Note: Arduino 'fixes' the forward references for you.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Kartman wrote:
Note: Arduino 'fixes' the forward references for you.
Which may make for "simplicity" when using Arduino but teaches very bad habits!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Indeed! Thanks to everybody!

Just to let you know...