Questions tagged «c++»

C ++是一种通用编程语言。它最初被设计为C的扩展,并且具有类似的语法,但是现在它是一种完全不同的语言。使用此标记可解决有关将要使用C ++编译器编译的代码的问题。对于与特定标准修订版[C ++ 11],[C ++ 14],[C ++ 17]或[C ++ 20]等相关的问题,请使用特定于版本的标记。

10
ACE vs Boost vs POCO [关闭]
已关闭。这个问题是基于观点的。它当前不接受答案。 想改善这个问题吗?更新问题,以便通过编辑此帖子以事实和引用的形式回答。 2年前关闭。 改善这个问题 我使用Boost C ++库已经有一段时间了。我绝对喜欢用于网络编程的Boost Asio C ++库。但是,我被介绍给另外两个库:POCO和自适应通信环境(ACE)框架。我想知道两者的优缺点。


2
如何在Makefile中定义几个包含路径
C ++的新手;对包括库,编译过程的基本了解。做了一些简单的makefile了。 我当前的项目涉及使用notifyix DB api,我需要在多个非标准目录中包含头文件。怎么写呢?Havent在网上发现了任何东西,可能是因为我没有使用好的搜索字词 这是我尝试的一种方式(不起作用)。只是为了显示makefile LIB=-L/usr/informix/lib/c++ INC=-I/usr/informix/incl/c++ /opt/informix/incl/public default: main main: test.cpp gcc -Wall $(LIB) $(INC) -c test.cpp #gcc -Wall $(LIB) $(INC) -I/opt/informix/incl/public -c test.cpp clean: rm -r test.o make.out
96 c++  include  makefile 

11
致命错误:Visual Studio中出现“无目标体系结构”
当我尝试在Win32或x64模式下使用Visual Studio 2010编译我的c ++项目时,出现以下错误: >C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\winnt.h(135): fatal error C1189: #error : "No Target Architecture" 我的预处理器定义说WIN32; _DEBUG; _CONSOLE;%(PreprocessorDefinitions) 是什么导致此错误,我该如何解决? // winnt.h: lines 127-136, MSVS says this is an inactive preprocessor block #if defined(_WIN64) #if defined(_AMD64_) #define PROBE_ALIGNMENT( _s ) TYPE_ALIGNMENT( DWORD ) #elif defined(_IA64_) #define PROBE_ALIGNMENT( _s ) (TYPE_ALIGNMENT( …

1
使用委托构造函数进行成员初始化
我已经开始尝试C ++ 11标准,我发现了这个问题,该问题描述了如何从同一类中的另一个ctor调用您的ctor,以避免使用init方法或类似方法。现在,我正在尝试使用类似如下的代码进行相同的操作: hpp: class Tokenizer { public: Tokenizer(); Tokenizer(std::stringstream *lines); virtual ~Tokenizer() {}; private: std::stringstream *lines; }; cpp: Tokenizer::Tokenizer() : expected('=') { } Tokenizer::Tokenizer(std::stringstream *lines) : Tokenizer(), lines(lines) { } 但这给了我错误: In constructor ‘config::Tokenizer::Tokenizer(std::stringstream*)’: /path/Tokenizer.cpp:14:20: error: mem-initializer for ‘config::Tokenizer::lines’ follows constructor delegation我尝试将Tokenizer()部分移到列表的第一和最后,但这无济于事。 这背后的原因是什么,我应该如何解决?我试着lines(lines)用this->lines = lines;代替将其移动到身体,并且效果很好。但是我真的很希望能够使用初始化列表。

6
为什么在C ++ 17中使用std :: make_unique?
据我了解,C ++ 14的引入std::make_unique是因为,由于未指定参数求值顺序,因此这是不安全的: f(std::unique_ptr<MyClass>(new MyClass(param)), g()); // Syntax A (说明:如果评估首先为原始指针分配内存,然后进行调用,g()并且在std::unique_ptr构造之前引发异常,则内存将泄漏。) 呼叫std::make_unique是一种限制呼叫顺序的方法,从而使事情变得安全: f(std::make_unique<MyClass>(param), g()); // Syntax B 从那时起,C ++ 17阐明了评估顺序,也使语法A变得安全,所以这是我的问题:在C ++ 17中仍然有理由使用std::make_uniqueover std::unique_ptr的构造函数吗?你能举一些例子吗? 到目前为止,我能想象的唯一原因是它只允许键入MyClass一次(假设您不需要依赖于的多态性std::unique_ptr<Base>(new Derived(param)))。但是,这似乎是一个很弱的原因,尤其是当的构造函数std::make_unique不允许指定删除器时std::unique_ptr。 只是要清楚一点,我不主张std::make_unique从标准库中删除(至少保持向后兼容才有意义),而是想知道是否仍然存在强烈推荐使用标准库的情况。std::unique_ptr
96 c++  c++17  unique-ptr 

7
如何在C ++ 11中输出枚举类的值
如何enum class在C ++ 11中输出an的值?在C ++ 03中,它是这样的: #include <iostream> using namespace std; enum A { a = 1, b = 69, c= 666 }; int main () { A a = A::c; cout << a << endl; } 在c ++ 0x中,此代码无法编译 #include <iostream> using namespace std; enum class A { a …

2
声明会影响std名称空间吗?
#include <iostream> #include <cmath> /* Intentionally incorrect abs() which seems to override std::abs() */ int abs(int a) { return a > 0? -a : a; } int main() { int a = abs(-5); int b = std::abs(-5); std::cout<< a << std::endl << b << std::endl; return 0; } 我希望输出将是-5and 5,但是输出是-5and …
96 c++  std  reserved 

6
为什么std :: initializer_list不是内置语言?
为什么没有std::initializer_list内置核心语言? 在我看来,它是C ++ 11的重要功能,但它没有自己的reserved关键字(或类似名称)。 相反,initializer_list它只是标准库中的模板类,它具有由编译器处理的新的braced-init-list语法的特殊隐式映射。 {...} 乍一看,这种解决方案是很棘手的。 现在是通过新的C ++语言实现方式吗:通过某些模板类的隐式角色而不是核心语言? 请考虑以下示例: widget<int> w = {1,2,3}; //this is how we want to use a class 为什么选择新班级: widget( std::initializer_list<T> init ) 而不是使用类似于以下任何想法的东西: widget( T[] init, int length ) // (1) widget( T... init ) // (2) widget( std::vector<T> init ) // (3) 一个经典的数组,您可能会在const这里和那里添加 …

8
在C ++中比较结构时找不到==运算符
比较以下结构的两个实例,我收到一个错误: struct MyStruct1 { MyStruct1(const MyStruct2 &_my_struct_2, const int _an_int = -1) : my_struct_2(_my_struct_2), an_int(_an_int) {} std::string toString() const; MyStruct2 my_struct_2; int an_int; }; 错误是: 错误C2678:二进制'==':未找到采用类型为'myproj :: MyStruct1'的左侧操作数的运算符(或没有可接受的转换) 为什么?

12
从头到尾迭代C ++向量
是否可以从头到尾迭代向量? for (vector<my_class>::iterator i = my_vector.end(); i != my_vector.begin(); /* ?! */ ) { } 还是只有这样才能做到: for (int i = my_vector.size() - 1; i >= 0; --i) { }
96 c++  vector  iterator 

9
找不到Dumpbin.exe
我的系统上没有看到dumpbin.exe。我的系统上装有Visual Studio 2005。当我在命令行上键入dumpbin时,它说无法识别的命令。 它是默认情况下随Visual Studio提供的,还是我必须显式添加此工具?

4
会将'::'替换为'。在C ++中造成歧义?
在C ++中,运算符::用于访问名称空间或类中的类,函数和变量。 如果使用语言规范.代替::访问对象的实例变量/方法时那样,而不是在那种情况下,那么是否会引起可能不存在的歧义::? 鉴于C ++不允许变量名也是类型名,因此我认为这种情况不会发生。 澄清:我不是问为什么::选择了.,即使它也可以工作?


4
使用不同对象时模板专业化的多种定义
当我在不同的目标文件中使用专用模板时,链接时出现“多个定义”错误。我发现的唯一解决方案涉及使用“内联”功能,但似乎有些解决方法。如何不使用“ inline”关键字解决该问题?如果那不可能,为什么呢? 这是示例代码: paulo@aeris:~/teste/cpp/redef$ cat hello.h #ifndef TEMPLATE_H #define TEMPLATE_H #include <iostream> template <class T> class Hello { public: void print_hello(T var); }; template <class T> void Hello<T>::print_hello(T var) { std::cout << "Hello generic function " << var << "\n"; } template <> //inline void Hello<int>::print_hello(int var) { std::cout << …
95 c++  templates 

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.