在下面的代码中,该行
const char * const * eNames (names+cntNames);
Run Code Online (Sandbox Code Playgroud)
导致Visual Studio 2008 中出现C2061错误:
语法错误:标识符'标识符' - 编译器找到了不期望的标识符.确保在使用之前声明了标识符.初始化器可以用括号括起来.要避免此问题,请将声明符括在括号中或使其成为typedef.当编译器将表达式检测为类模板参数时,也可能导致此错误; 使用typename告诉编译器它是一个类型.
如果我换到
const char * const * eNames = names+cntNames;
Run Code Online (Sandbox Code Playgroud)
它没有抱怨.这是编译器错误吗?如果没有,为何投诉?
我的关于框说:版本9.0.30729.1 SP
我与GCC的同事没有看到这个错误.
#include <string>
#include <algorithm>
#include <functional>
#include <iostream>
namespace ns1 {
struct str_eq_to
{
str_eq_to(const std::string& s) : s_(s) {}
bool operator()(const char* x) const { return s_.compare(x)==0; }
const std::string& s_;
};
static bool getNameIndex(const char * const * names, size_t cntNames, const std::string& nm, int &result)
{ …Run Code Online (Sandbox Code Playgroud) 考虑以下C++ 11代码:
struct C {};
void f(int(C));
Run Code Online (Sandbox Code Playgroud)
类型是否f相同:
typedef int T(C);
void f(T);
Run Code Online (Sandbox Code Playgroud)
或者是这样的:
void f(int C);
Run Code Online (Sandbox Code Playgroud)
也就是说,应该将(C)其解释为declarator参数名称的一个C,还是作为abstract-declarator函数参数的一个?
标准中指定的是哪里?
最近我遇到了一个问题,不知何故(但只是某种程度上)对我有意义.它基于解释临时的构造作为单个(!)构造函数参数的声明.请看下面的最小例子.
#include <iostream>
class Foo0{
public:
Foo0(int a){};
void doStuff() {std::cout<<"maap"<<std::endl;};
};
class Foo1{
public:
Foo1(int a){};
void doStuff() {std::cout<<"maap"<<std::endl;};
};
class Foo2{
public:
Foo2(int a){};
void doStuff() {std::cout<<"maap"<<std::endl;};
};
class Bar{
public:
Bar(Foo0 foo0, Foo1 foo1, Foo2 foo2){};
};
int main () {
int x = 1;
Bar bar0(Foo0(x), Foo1(x), Foo2(x)); // Does not work: conflicting declaration ‘Foo1 x’ previous declaration as ‘Foo0 x’; conflicting declaration ‘Foo2 x’ previous declaration as ‘Foo0 x’
Bar bar1(Foo0{x}, Foo1(x), Foo2(x)); // Works …Run Code Online (Sandbox Code Playgroud) 我有一个向量,我试图执行包含函数.我收到某种铸造错误,我无法拼凑出一个解决方案.我也想知道我在做什么是检查向量是否包含值的适当方法.
这是代码:
#include "stdafx.h"
#include <vector>
static void someFunc(double** Y, int length);
static bool contains(double value, std::vector<double> vec);
int main()
{
double doubleArray[] = { 1, 2, 3, 4, 5 };
double *pDoubleArray = doubleArray;
int size = sizeof doubleArray / sizeof doubleArray[0];
someFunc(&pDoubleArray, size);
return 0;
}
static void someFunc(double** Y, int length)
{
std::vector<double> vec();
for(int i = 0; i < 10; i++)
{
//error: 'contains' : cannot convert parameter 2 from 'std::vector<_Ty> (__cdecl *)(void)' to 'std::vector<_Ty>'
if(contains(*(Y[i]), …Run Code Online (Sandbox Code Playgroud) 我在下面有一个示例代码.
#include<iostream>
template<typename T>
class XYZ
{
private:
T & ref;
public:
XYZ(T & arg):ref(arg)
{
}
};
class temp
{
int x;
public:
temp():x(34)
{
}
};
template<typename T>
void fun(T & arg)
{
}
int main()
{
XYZ<temp> abc(temp());
fun(temp()); //This is a compilation error in gcc while the above code is perfectly valid.
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,即使XYZ构造函数将参数作为非const引用,它也会很好地编译,而fun函数无法编译.这是特定于g ++编译器还是c ++标准必须要说些什么呢?
编辑:
g ++ -v给出了这个.
gcc版本4.5.2(Ubuntu/Linaro 4.5.2-8ubuntu4)
可能重复:
为什么没有调用构造函数?
我正在使用Visual Studio 2012,假设Test是一个类
class Test
{
};
Run Code Online (Sandbox Code Playgroud)
当我创建一个新的Test实例时,以下两种方式的区别是什么?
方式1
Test t;
Run Code Online (Sandbox Code Playgroud)
方式2
Test t();
Run Code Online (Sandbox Code Playgroud)
我在下面的代码中得到了这个问题,最初,我在方法2中定义了A的实例,我只得到一个错误,因为B没有提供默认构造函数,但是当我以方式1定义它时,我得到了一个额外的错误.
class B
{
B(int i){}
};
class A
{
A(){}
B b;
};
int main(void)
{
A a(); // define object a in way 2
getchar() ;
return 0 ;
}
Run Code Online (Sandbox Code Playgroud)
如果我定义一个方式1
A a;
Run Code Online (Sandbox Code Playgroud)
我会得到另一个错误说
错误C2248:'A :: A':无法访问在类'A'中声明的私有成员
所以我猜两种方式之间肯定存在一些差异.
我有一个问题:ClassName instance()在C++中创建类的实例时使用了什么构造函数?
例:
#include <iostream>
using namespace std;
class Test
{
private:
Test()
{
cout << "AAA" << endl;
}
public:
Test(string str)
{
cout << "String = " << str << endl;
}
};
int main()
{
Test instance_1(); // instance_1 is created... using which constructor ?
Test instance_2("hello !"); // Ok
return 0;
}
Run Code Online (Sandbox Code Playgroud)
谢谢 !
我想vector<string通过使用其范围构造函数将文本文件中的所有行加载到a中,然后通过cout以下方式输出:
#include<iostream>
#include<fstream>
#include<vector>
#include<iterator>
using namespace std;
int main()
{
ifstream file("file.txt");
vector<string> strings(istream_iterator<string>(file) , istream_iterator<string>());
for(auto s : strings)
cout << s << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在尝试编译上面的代码时,我得到了几个错误,例如:
error: no matching function for call to ‘begin(std::vector<std::basic_string<char> > (&) (std::istream_iterator<std::basic_string<char> >, std::istream_iterator<std::basic_string<char> > (*) ()))’
for(auto s : strings)
^
Run Code Online (Sandbox Code Playgroud)
还有其他几个......
我想我在这里遗漏了一些明显的东西,有人可以帮忙吗?
我上课了
class A
{
public:
class Key
{
Key() {}
Key(Key const &) {}
};
A(Key key, int a = 5) {}
};
Run Code Online (Sandbox Code Playgroud)
构造函数Key是私有的,因此没有人应该能够构造一个对象A.但是,使用以下代码:
int main() {
A a(A::Key()); // this compiles !!!
A a2(A::Key(), 5); // this doesn't
// somehow defaulting the argument causes the private constructor
// to be OK - no idea why
return 0;
}
Run Code Online (Sandbox Code Playgroud)
通过int a在我的构造函数中使用默认参数,编译器很乐意编译我的用法,A::Key()尽管它是私有的.但是,如果我明确地给出了一个值a,那么编译器会正确识别我正在尝试使用私有构造函数并输出错误.为什么是这样?有没有办法强制编译器在第一个例子中出错?
请看这里的实例.
看完路易斯·布兰迪在CppCon 2017上的演讲后,我惊讶地发现这段代码实际编译:
#include <string>
int main() {
std::string(foo);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
由于某种原因,std::string(foo)它与std::string foo声明变量相同.我发现它绝对违反直觉,并且看不出C++以这种方式工作的任何理由.我希望这会给出一个关于未定义标识符的错误foo.
它实际上使表达式token1(token2)具有比我之前想象的更多可能的解释.
所以我的问题是:这种恐怖的原因是什么?这个规则什么时候真的有必要?
PS对不起措辞不好的标题,请随时更改!
c++ ×10
c++11 ×2
constructor ×2
vector ×2
casting ×1
class ×1
construction ×1
fstream ×1
private ×1
temporary ×1