相关疑难解决方法(0)

在C++标识符中使用下划线有哪些规则?

在C++中通常用某种前缀命名成员变量来表示它们是成员变量而不是局部变量或参数.如果你来自MFC背景,你可能会使用m_foo.我myFoo偶尔也见过.

C#(或者可能只是.NET)似乎建议只使用下划线,如_foo.这是否允许C++标准?

c++ standards naming-conventions c++-faq

906
推荐指数
4
解决办法
24万
查看次数

转发声明一个STL容器?

是否可以在头文件中转发声明STL容器?例如,请使用以下代码:

#include <vector>

class Foo
{
private:
    std::vector<int> container_;
    ...
};
Run Code Online (Sandbox Code Playgroud)

我希望能够做到这样的事情:

namespace std
{
    template <typename T> class vector;
}

class Foo
{
private:
    std::vector<int> container_;
    ...
};
Run Code Online (Sandbox Code Playgroud)

可以这样做吗?

c++ header c++-standard-library

47
推荐指数
3
解决办法
2万
查看次数

C++前向声明问题

我有一个头文件,有一些前向声明但是当我在实现文件中包含头文件时,它包含在先前的前向声明的包含之后,这会导致像这样的错误.

error: using typedef-name ‘std::ifstream’ after ‘class’
/usr/include/c++/4.2.1/iosfwd:145: error: ‘std::ifstream’ has a previous declaration.

class ifstream;

class A
{
    ifstream *inStream;
}
// End of A.h

#include <ifstream>
using std::ifstream;

#include "A.h"

// etc
Run Code Online (Sandbox Code Playgroud)

什么是解决这个问题的常态?

提前致谢.

c++ forward-declaration

12
推荐指数
1
解决办法
5918
查看次数

C++模板交叉定义

我想知道在C++中是否可以这样做:

 template <typename T> class A { T link;};
 template <typename U> class B { U link;};

 class AA : A<BB> {};
 class BB : B<AA> {};
Run Code Online (Sandbox Code Playgroud)

因为它会产生错误:

 error: ‘BB’ was not declared in this scope
 error: template argument 1 is invalid
Run Code Online (Sandbox Code Playgroud)

我试图使用预期声明:

 class AA;
 class BB;

 class AA : A<BB> {};
 class BB : B<AA> {};
Run Code Online (Sandbox Code Playgroud)

但它不起作用:

 In instantiation of ‘A<AA>’:
 error: ‘A<T>::s’ has incomplete type
 error: forward declaration of ‘struct AA’
Run Code Online (Sandbox Code Playgroud)

谢谢您的帮助,

c++ templates

0
推荐指数
1
解决办法
311
查看次数