C++标准指出,返回对局部变量的引用(在堆栈上)是未定义的行为,那么为什么当前编译器中的许多(如果不是全部)只给出这样做的警告?
struct A{
};
A& foo()
{
A a;
return a; //gcc and VS2008 both give this a warning, but not a compiler error
}
如果编译器给出错误而不是警告这段代码,难道不是更好吗?
允许只带警告地编译这段代码有什么好处吗?
请注意,这与const引用无关,后者可以将临时引用的生存期延长到引用本身的生存期。
我正在编写一个小型c++程序,其结构类似于以下内容:
class A {
B * someObjects;
};
typedef A* APointer;
struct B{
APointer a;
int n;
}
试图编译这会产生一个“标识符是未定义的”错误,因为在A类中不知道结构B,否则在A类之前声明结构B仍然会出现类似的错误,因为B不知道APointer,或者APointer不知道A。是否有可能使A类和结构B成为好朋友?提前感谢!
我是C++的初学者。我写了以下文章:
// GradeBook.h
#include <iostream>
#include <string>
using namespace std;
class GradeBook {
public:
GradeBook(string); // constructor that initializes courseName
void setCourseName(string); // function that sets the course name
string getCourseName(); //
我正在做一个项目,主程序用C写,它是在一个嵌入式Linux系统上。在另一家公司提供的硬件中,我得到了它们的lib(c和c++语言中的静态库).for,后来移植到其他设备上,我制作了新的libs (共享库)来链接到c语言中的应用程序,即:
their libs(static libs,c/c++) --> my libs(shared libs,c) --> my applications(c).
所有的c++静态库都对我有好处,在使用c++库时,我的库编译得很好,但是当链接到我的应用程序时会出现两个错误:
libplate.so:对operator delete(void*)的
我的系统上的SSL_library_init()文档声明:
NOTES
SSL_library_init() must be called before any other action takes place.
SSL_library_init() is not reentrant.
我对使用散列函数的EVP套件感兴趣。例如EVP_DigestInit_ex,EVP_DigestUpdate和EVP_DigestFinal_ex。
EVP_DigestInit_ex和其他的在libcrypto,而SSL_library_init()在libssl。我想尽量减少链接库的数量。如果
在C#中,我可以让两个嵌套类相互引用,没有问题:
public class CFGFile
{
class Setting
{
public Entry Ent;
}
class Entry
{
public Setting Setg;
}
}
但是,在C++中尝试相同的操作会导致问题:
class CFGFile
{
class Setting;
class Entry
{
Setting Setg;
};
class Setting
{
Entry Ent;
]
};
我得到了
“不允许使用不完整类
我有一个基类A,(在C++中),并且希望有另一个继承A类的类B。
class A{
public:
int n;
A(){n=0;}
void Test(int m);
};
class B:public A{
public:
int func();
};
我有A.cpp和B.cpp,分别。但是,当我在cygwin中编译B.cpp时,会弹出一个错误,说函数的“未定义引用”属于A,所以我想知道如何在cygwin中定义#include"A.h“?
可能重复:
我最近一直在和C++打交道。目前,我正在尝试编写至少每个人都做过一次的程序:一个简单的LinkedList类。代码已经完成,但我不知怎么没能编译它。我一直在谷歌上搜索,似乎我把目标文件链接错了。我的代码就是这样的:
test.cpp
#include "linkedlist.h"
int main()
{
LinkedList<int> list;
// do something
}
linkedlist.h
template <typename T>
class LinkedList
{
// a lot
请考虑以下示例:
#include <new>
struct FunctionObject
{
int operator()() // non-const, modifies the state of this object
{
i += 1;
j += 2;
return i + j;
}
int i = 0;
int j = 0;
};
struct Wrapper
{
explicit Wrapper(const FunctionObject& input_objec