2013-06-13 7 views
22

Dlatego chcę dodać struct z pliku nagłówkowego c jako członka klasy do klasy C++. Ale pojawia się błąd kompilatora dla pliku cpp: bar was not declared inn this scope. To jest to, co mam:Prawidłowy sposób inicjowania struct w konstruktorze klasy

// myClass.hpp 
#include fileWithStruct.h 

class myClass 
{ 
    public: 
     struct foo bar; 
}; 


//myClass.cpp 

#include "myClass.hpp" 

//Initialize structure in Constrcutor 
myClass::myClass() 
{ 
    bar = {1, 0, "someString", 0x4}; 
} 

Odpowiedz

6

inicjalizacji należy zrobić w ten sposób (C++ 11):

myClass::myClass() 
: bar{1, 0, "someString", 0x4} 
{ 

} 

Ponadto, nie zapomnij, aby zadeklarować konstruktor w definicji klasy.

27

C++ 03 Style

#include "fileWithStruct.h" 
/* say the contents were 
struct foo 
{ 
    int foo1; 
    float foo2; 
}; 
*/ 

class myClass 
{ 
    public: 
     int val; 
     foo bar; 
     // since foo is a POD-struct (a.k.a C struct), no constructor would be present 
     // however bar() will zero-initialize everything in the struct 
     myClass() : val(), bar() 
     { 
     } 
}; 

parentheses following bar matters. Zajrzyj na stronę value and zero-initialization, aby zrozumieć, dlaczego to działa. Należy zauważyć, że dodając konstruktora do myClass, stworzyliśmy go jako typ inny niż POD. Aby obejść ten problem, można zachować myClass jako kruszywo i napisać:

class myClass 
{ 
    public: 
     int val; 
     foo bar; 
}; 

int main() 
{ 
    myClass zeroed_obj = { }; 
    myClass inited_obj = { 2, {0, 1.0f} }; 
    myClass partially_inited_obj = { 2 }; // equivalent to {2, {}}; which would zero all of myClass::bar 
    myClass garbage_obj; // warning: when left uninitialized, every member without a constructor will end up with garbage value 
} 

C++ 11 Style

class myClass 
{ 
public: 
    // default member initializations 
    int val = { };   // zero-initialization 
    foo bar = { 0, 0.0f }; // aggregate-initializing foo here, just giving { } will zero all of myClass::bar 

    // should you want to receive an element from the constructor, this can be done too 
    // aggregate initializing a struct in constructor initialization list is allowed from C++11 onwards 
    // in C++03, we would've resorted to just setting the member of bar inside the constructor body 
    myClass(int _foo1) : bar{_foo1, 0.f}, val{} 
    { 
    } 

    // since we've a non-default constructor, we've to re-introduce the default constructor 
    // if we need the above in-class initialization to work 
    myClass() = default; 
}; 

Tutaj używamy C++ 11 na uniform initialization syntax. Jednakże, robiąc to, myClass staje się typem nie POD; Inicjowanie członków jest podobne do dodawania konstruktora do klasy, co powoduje, że myClass jest klasą niebanalną, ale standardową. Zgodnie z C++ 11 dla klasy, która ma być POD, powinna być zarówno trywialna, jak i standardowa. Zamiast tego zachowuje się

#include "fileWithStruct.h" 
#include <type_traits> 
#include <iostream> 

class myClass 
{ 
public: 
    int val; 
    foo bar; 
}; 

int main() 
{ 
    myClass obj { }; // initializes val, bar.foo1 and bar.foo2 to 0 
    myClass m { 0, {1, 2.0f} }; // initilizes each member separately 
    std::cout << std::is_pod<myClass>::value << std::endl; // will return 1 
} 
jako czujnik POD myClass.

Aby dowiedzieć się więcej o agregatach i czujnikach POD, przeczytaj artykuł this excellent post.

+3

Nie masz n eed do sat 'struct foo bar;' w C++ 03 lub C++ 11, po prostu 'foo bar;' zrobi. Również w C++ 11 możesz zainicjować 'bar' w punkcie deklaracji. – juanchopanza

+1

Uzgodniono, poprawiono w odpowiedzi. To jest stara składnia deklaracji struktury obiektów w stylu C. – legends2k

+1

OP wyraźnie wspomina o 'foo' znajdującym się w nagłówku C, więc dodanie konstruktora do niego nie jest opcją. (I proszę nie naprawiaj tego, wstawiając '#ifdef __cplusplus' do nagłówka C) –

7

To, co tu robisz, to zadanie, a nie inicjalizacja. Inicjowanie dzieje się w liście inicjującej konstruktora, przed ciele konstruktora, czy w C++ 11 do inicjowania tuż po zarejestrował deklaracji zmiennej:

myClass.hpp, ogólnym przypadku:

/** you might want to do this if you are linking 
* against the C lib or object file of that header: 
*/ 
extern "C" { 
    #include fileWithStruct.h 
} 

class myClass 
{ 
public: 
    foo bar; //no need for "struct" in C++ here 
}; 

C++ 11:

myClass.cpp

#include "myClass.hpp" 

//Initialize structure in Constrcutor 
myClass::myClass() 
    : bar{1, 0, "someString", 0x4} 
{} 

opcja Antoher jest dostarczenie wartość początkową foo z nawiasów lub równy-inicjatora na członie deklaracji zmiennej:

myClass.hpp

extern "C" { 
    #include fileWithStruct.h 
} 

class myClass 
{ 
public: 
    foo bar{1, 0, "someString", 0x4}; 
}; 

W tym przypadku, trzeba nie definiuje konstruktora, ponieważ jest on generowany niejawnie przez kompilator (w razie potrzeby), poprawnie inicjując bar.

C++ 03:

Tutaj łączna inicjalizacji w listach startowych nie jest dostępny, więc trzeba korzystać z obejścia, np:

myClass.cpp

#include "myClass.hpp" 

//Initialize structure in Constrcutor 
myClass::myClass() 
    : bar() //initialization with 0 
{ 
    const static foo barInit = {1, 0, "someString", 0x4}; //assignment 
    bar = barInit; 
} 

Lub:

#include "myClass.hpp" 
namespace { 
    foo const& initFoo() { 
    const static foo f = {1, 0, "someString", 0x4}; 
    return f; 
    } 
} 

//Initialize structure in Constrcutor 
myClass::myClass() 
    : bar(initFoo()) //initialization 
{ } 
1

Musisz to określić struktura foo powinna mieć "C-linkage". Poniżej znajduje się pełny przykład.

// fileWithStruct.h 
#ifdef __cplusplus 
extern "C" { // Declare as extern "C" if used from C++ 
#endif 

typedef struct _foo 
{ 
    int a; 
    int b; 
    const char* c; 
    int d; 
} foo; 


#ifdef __cplusplus 
} 
#endif 

Plik nagłówka myClass:

// myClass.hpp 
#include "fileWithStruct.h" 

class myClass 
{ 
public: 
    myClass(); 

    foo bar; 
}; 

Realizacja C + 11 myClass który używa rozszerzonych list initializer:

// myClass.cpp 
#include "myClass.hpp" 

myClass::myClass() 
    : bar({1, 0, "someString", 0x4}) 
{ 
} 

... a wersja C++ 03, jeśli nie przeniosłeś się jeszcze do C++ 11:

#include "myClass.hpp" 

myClass::myClass() 
    : bar() 
{ 
    bar.a = 1; 
    bar.b = 0; 
    bar.c = "someString"; 
    bar.d = 0x4; 
} 
+3

C++ 0x to C++ 11 ... – Griwes

Powiązane problemy