2013-06-13 52 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

相关问题