struct in einem Object

OP #5915107
Lesenswert?
• ▲
▼
Hallo
wie macht man das in C++
Ich habe eine Klasse

class KeyboardClass
{
 protected:
   struct tButton {
     uint16_t x;
     uint16_t y;
     uint16_t h;
     uint16_t w;
     uint16_t KeyCol;
     uint16_t Shadow;
     uint16_t SColor;
     uint16_t TColor;
   };

struct tButton foo();
}

..............
struct tButton KeyboardClass::foo()
{

Der Compiler meckert mit

Keyboard.cpp: 32:16: error: prototype for 'tButton KeyboardClass::foo()' 
does not match any in class 'KeyboardClass
 struct tButton KeyboardClass*: foo()
Error compiling project sources

Keyboard.cpp:5: In file included from

Keyboard.h: 108:18: error: candidate is: KeyboardClass::tButton 
KeyboardClass::foo()
   struct tButton foo()
Build failed for project '_Keyboard'

Was ist falsch an dem Prototype ???
Gast #5915123
Lesenswert?
• ▲
▼
Musst du vielleicht für den return type von foo auch KeyboardClass:: 
angeben, wie vom Complier vorgeschlagen? Der Typ wurde ja innerhalb der 
Klasse definiert.
Soweit ich weiß, kannst du bei C++ das Schlüsselwort "struct" bei der 
Verwendnung des Structs auch weglassen.
Gast #5915125
Lesenswert?
• ▲
▼
Na Ausserhalb der Klasse/des Namespace von KeyboardClass, musst du für 
alles, was sich auf innerhalb des KeyboardClass namespace bezieht, 
explizit angeben, dass es dort drin ist. Das sagt dir ja sogar schon der 
Compiler in der Fehlermeldung. tButton ist im Namespace KeyboardClass, 
also musst du ausserhalb von KeyboardClass für das tButton  darin 
KeyboardClass::tButton schreiben. Also:
1
class KeyboardClass
2
{
3
 protected:
4
   struct tButton {
5
     uint16_t x;
6
     uint16_t y;
7
     uint16_t h;
8
     uint16_t w;
9
     uint16_t KeyCol;
10
     uint16_t Shadow;
11
     uint16_t SColor;
12
     uint16_t TColor;
13
   };
14

15
struct tButton foo();
16
}
17

18
..............
19
struct KeyboardClass::tButton KeyboardClass::foo()
20
{

Und weil das hier C++ ist, könnte man das struct bei der Verwendung von 
tButton glaub ich sogar weglassen.
Persönliche Seite #5915141
Lesenswert?
• ▲
▼
Vancouver schrieb:
> Soweit ich weiß, kannst du bei C++ das Schlüsselwort "struct" bei der
> Verwendnung des Structs auch weglassen.

DPA schrieb:
> Und weil das hier C++ ist, könnte man das struct bei der Verwendung von
> tButton glaub ich sogar weglassen.

Ja, das kann man in C++ immer weglassen, es sei denn es gibt eine 
gleichnamige Funktion.
Außerdem gibt es in C++ keine structs, sondern nur Klassen - mit dem 
"struct" Schlüsselwort definiert man eine Klasse, in der alles per 
default public ist (und nicht private wie bei per "class" definierten 
Klassen). struct und class zu mischen ist daher nicht unbedingt 
sinnvoll.

Siehe https://stackoverflow.com/a/34108140/4730685
OP #5915417
Lesenswert?
• ▲
▼
Nochmal Danke
Ich habe das nun so definiert, compiliert nun ohne Probleme ist das
OK so ?????

struct tButton {
  uint16_t x;
  uint16_t y;
  uint16_t h;
  uint16_t w;
  uint16_t KeyCol;
  uint16_t Shadow;
  uint16_t SColor;
  uint16_t TColor;

};


class KeyboardClass
{
 protected:
tButton _Button;

Antwort schreiben

Bitte melde dich an, um einen Beitrag zu schreiben.

oder

Mit Google-Account einloggen

Die Registrierung ist kostenlos und dauert nur eine Minute.

Jetzt registrieren