这段代码
#include <memory>
#include <vector>
class Foo
{
public:
~Foo()
{
}
std::unique_ptr<int> bar;
};
int main()
{
std::vector<Foo> foos;
foos.emplace_back();
}
在g++中产生以下错误消息:
In file included from /usr/include/c++/4.8/memory:64:0,
f
我编写了两个类,我在将它们放入向量时遇到了问题:
#include <vector>
#include <iostream>
#include <fstream>
class A
{
public:
std::ofstream filestream;
int aa1;
int aa2;
int aa3;
int aa4;
int aa5;
int aa6;
int aa7;
int aa8;
int aa9;
int aa10;
A() {}
~A()
在C中,下面的代码编译得很好。
typedef struct T {
int val;
} T;
void test(volatile T*p, T a) {
*p = (volatile T)a; // doesn't work for G++
*p = a; // doesn't work for G++
}
但在C++ (由gcc 12.2测试)中,这是非法的:
<source>: In function 'void test(volatile T*, T)':
<source>:29:22: erro
我想防止我的类的用户将它用作自动变量,所以我编写了这样的代码:
class A {
private:
~A() = default;
};
int main() {
A a;
}
我希望代码不会被编译,但是g++可以正确地编译它。
但是,当我将代码更改为:
class A {
private:
~A(){}
};
int main() {
A a;
}
现在,g++给出的错误是~A()是私有的,这是我的期望。
"= default“析构函数和空析构函数有什么区别?
根据"Inside C++对象模型“,编译器只在以下四种条件中的任何一种为真时才生成副本构造函数(如果不是程序员声明的话):
类包含存在副本构造函数的类的成员对象时(类设计器显式声明(如前一个字符串类,或由编译器合成,如类Word),当类派生自存在副本构造函数的基类时(再次声明或synthesized)When类声明一个或多个虚拟函数,当类派生自一个或多个基类为虚拟的继承链时)。
这意味着如果我有一个只有构造函数的类,那么编译器就不会提供复制构造函数。
让我们举一个例子:
class test
{
test(){}
};
int main()
{
test obj1;
我正在尝试从指定女巫类型的模板类继承一个非模板类
我的代码是这样的:
基本class(updated):的头文件
//base.hpp
template<typename T>
class Base {
public:
Base(T a,int b) :
aa(b) {
this->bb = a;
// ... some code
}
// .. some functions ( all are NOT virtual )
protected:
const int aa;
T bb
所以,在玩C++的时候,我做了这样的事情:
#include <iostream>
int main() {
std::string s{"someThing"};
std::cout << " s is: " << s << '\n';
s = 97;
std::cout << " s is: " << s << '\n';
return 0;
}
当我用g++编译它时,完美地编译了,并且在运行
斯蒂芬·普拉塔( Stephen )在他的书“C++ Primer Plus p 31”中说:
许多现有程序使用经典的C函数头:经典C下的main() // original C style,省略返回类型与声明函数为int类型相同。然而,C++已经逐步取消了这种使用。
然而,C++11草案3.6.1->2说
实现不应预定义主要功能。此功能不应超载。它应该有一个int类型的返回类型,但否则它的类型是实现定义的。
测试结果
$ g++ -Werror=pedantic MainCheck.cpp -o MainCheck
MainCheck.cpp:3:6: error: I
考虑一下这门课:
class Base{
public:
void func(double a) = delete;
void func(int a) const {}
};
int main(){
Base base;
base.func(1);
return 0;
}
使用clang++编译时,它会产生以下错误:
clang++ --std=c++11 test.cpp
test.cpp:22:7: error: call to member function 'func' is ambiguous
base.func(
今天我发现了C和C++的不同之处。我将程序分隔成两个不同的文件,这是我的C测试:
/* in file main.c*/
#include <stdio.h>
int main()
{
int a = 3, b = 4;
int c = sum(a, b);
printf("%d\n", c);
}
和
/* In file sum.c */
#include <stdio.h>
int sum(int x, int y)
{
return x + y;
}
然后,我编译了他们的gcc main.c sum.c,没有错
在c#中,静态成员对于每个泛型类都是唯一的,如下例所示
using System;
//A generic class
public class GenTest<T>
{
//A static variable - will be created for each type on refraction
static CountedInstances OnePerType = new CountedInstances();
//a data member
private T mT;
//simple constructor
public GenTest(
使用此代码:
struct A
{
int i;
const int b;
};
// The union is to verify that A is a type that can be used in a union.
union U
{
A a;
int b;
};
int main()
{
U a = {1, 1};
U b = {2, 1};
}
g++版本4.8.3抱怨错误:
a.cpp:9:4: error: member ‘A U::a’ with copy assignment operator not allowed i
我在主函数之后又声明了全局变量,但它仍然影响主函数。我知道C允许在第一次声明没有初始化变量时再次声明全局变量(它在c++中不起作用)。如果在主函数之后赋值,它仍然可以在c中使用两个警告,但在c++中会出现错误。
我已经调试了代码,但它从未到达int a=10;行。
#include <stdio.h>
#include <string.h>
int a;
int main()
{
printf("%d",a);
return 0;
}
/*a=10 works fine with following warnings in c.
我有一些困难,让我的程序建设使用一个makefile灵感来自于的文件。
我对makefile非常陌生,我能够让它构建得很好,但是有几件事情不起作用,据我理解,应该是起作用的。我希望你们能为我澄清这件事!
我在运行OSX。使用g++编译(或至少打算编译)。这是相关的makefile和depend.sh
makefile
CC = g++
MODULES := src Engine Engine/core Engine/render
# look for include files in
# each of the modules
CFLAGS := $(patsubst %,-I%,$(MODU
为什么使用内联朋友的地址不会产生代码。这适用于常规的内联函数。声明是否不符合内嵌朋友的要求?
#include <iostream>
namespace S
{
template <unsigned N>
class X
{
int i;
friend X operator+(const X& a, const X& b) noexcept
{
(void)a;
(void)b;
return {};
}
};
inline X<256> operator