2013-06-13 58 views
22

所以我想從c頭文件中將一個結構作爲類成員添加到C++類中。 但是我得到cpp文件的編譯器錯誤:bar was not declared inn this scope。 這是我有:在類構造函數中初始化結構的正確方法

// 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}; 
} 

回答

6

初始化應該做這樣(C++ 11):

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

} 

而且,不要忘了申報你的構造你的類定義。

27

C++ 03風格

#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。請參閱value and zero-initialization以瞭解其原因。需要注意的是,通過向myClass添加構造函數,我們已將其設置爲非POD類型。要解決這個問題,可以保留myClass爲骨料,寫:

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風格

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; 
}; 

這裏我們使用C++ 11的uniform initialization syntax。但是,通過這樣做myClass變成非POD類型;成員初始化類似於向該類添加構造函數,從而使myClass成爲一個不重要但標準佈局的類。按照C++ 11,一個類是POD,它應該既簡單又標準。反而做

#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 
} 

將保留myClass作爲POD。

請參考this excellent post瞭解更多關於聚合物和POD的信息。

+3

你沒有n在C++ 03或C++ 11中,只需'foo bar;'就可以了。另外,在C++ 11中,你可以在聲明處初始化'bar'。 – juanchopanza

+1

同意,修復它的答案。這是舊的C風格結構對象聲明語法。 – legends2k

+1

OP明確提到'foo'在C頭文件中,因此向它添加一個構造函數不是一個選項。 (請不要通過將'#ifdef __cplusplus'放入C頭來解決這個問題) –

7

你在那裏做什麼是分配,而不是初始化。初始化發生在構造函數的初始化列表,構造體之前,或在C++ 11中的初始值設定的成員變量聲明之後:

myClass.hpp,一般情況下:

/** 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} 
{} 

Antoher選擇是提供foo的初始值與在成員變量聲明撐 - 或等於初始值設定

myClass.hpp

extern "C" { 
    #include fileWithStruct.h 
} 

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

在這種情況下,需要不定義構造函數,因爲它由編譯器隱式生成(如果需要),正確初始化bar

C++ 03:

在初始化列表在這兒集合初始化不可用,所以你需要使用的變通辦法,如:

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; 
} 

或者:

#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

您需要指定foo結構應該有「C-linkage」。以下是一個完整的例子。

// 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 

在myClass頭文件:

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

class myClass 
{ 
public: 
    myClass(); 

    foo bar; 
}; 

的C + 11執行MyClass的的它採用擴展的初始化列表:

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

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

...和C++ 03的版本,如果你還沒有移動到C++ 11:

#include "myClass.hpp" 

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

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

相關問題