我找到了一个不起眼的日志记录错误,事实上长度为2的初始化列表似乎是一个特例!这怎么可能?
代码是使用Apple LLVM版本5.1(clang-503.0.40)编译的CXXFLAGS=-std=c++11 -stdlib=libc++.
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
typedef vector<string> Strings;
void print(string const& s) {
printf(s.c_str());
printf("\n");
}
void print(Strings const& ss, string const& name) {
print("Test " + name);
print("Number of strings: " + to_string(ss.size()));
for (auto& s: ss) {
auto t = "length = " + to_string(s.size()) + ": " + s;
print(t);
}
print("\n");
}
void test() {
Strings a{{"hello"}}; print(a, "a");
Strings b{{"hello", "there"}}; print(b, "b");
Strings c{{"hello", …Run Code Online (Sandbox Code Playgroud) 类内初始值设定项(C++ 11特性)必须用花括号括起来或跟在=符号后面.它们可能未在括号内指定.
这是什么原因?
有这样的程序:
#include <iostream>
#include <string>
using namespace std;
class test
{
public:
test(std::string s):str(s){};
private:
std::string str;
};
class test1
{
public:
test tst_("Hi");
};
int main()
{
return 1;
}
Run Code Online (Sandbox Code Playgroud)
...为什么我执行时会得到以下内容
g ++ main.cpp
main.cpp:16:12: error: expected identifier before string constant
main.cpp:16:12: error: expected ‘,’ or ‘...’ before string constant
Run Code Online (Sandbox Code Playgroud) C++ 11允许类内初始化:
struct Foo{
std::vector<std::string> v{3}; // vector of 3 empty strings
};
Run Code Online (Sandbox Code Playgroud)
如果我们想要在类中初始化一个int的向量,我们会得到其他东西:
struct Foo{
std::vector<int> v{3}; // vector of one element with value 3
};
Run Code Online (Sandbox Code Playgroud)
这个问题似乎是语言的限制,正如之前的问题所讨论的那样.但是,如果这不是类内初始化,我们将能够使用括号而不是大括号,并获得所需的结果:
std::vector<int> v(3); // vector of three zeros
Run Code Online (Sandbox Code Playgroud)
但是,由于最令人烦恼的解析,我们不能在课堂上这样做:
struct Foo{
std::vector<int> v(3); // most vexing parse; doesn't compile
};
Run Code Online (Sandbox Code Playgroud)
当然,上面的代码是否是良好的设计实践是有争议的,因为我们可以轻松地将我们想要做的事情移动到构造函数中.但暂时把它放在一边,有没有办法执行所需的初始化,尽可能接近第一个std::string例子,这没有问题?
我在类的私有成员变量中有一行代码:
vector<double> dQdt(3)
Run Code Online (Sandbox Code Playgroud)
在xcode中编译时,会出现"预期参数声明符"错误.我想我提供了足够的信息.我没有看到这个声明有什么问题.
#include <iostream>
#include <vector>
struct S {
//std::vector<int> ns(1); //ERROR!
std::vector<int> ns = std::vector<int>(1);
};
int main() {
S s;
std::cout << (s.ns[0] = 123) << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
使用括号初始值设定项似乎是一个错误.这背后的目的是什么?
例如:
struct Spell
{
int id;
vector<int> cost(4);
};
Run Code Online (Sandbox Code Playgroud)
它说
'Expected parameter declaration'.
Run Code Online (Sandbox Code Playgroud)
另外这段代码有什么区别吗?
if (can(vector <int>inv, spell.cost))
{
cout << "CAST " << spell.id << endl;
done = true;
break;
}
Run Code Online (Sandbox Code Playgroud)
这个说
'Expected '(' for function-style cast or type construction'
Run Code Online (Sandbox Code Playgroud)
有机会可以帮我一下吗?
class data
{
private:
int ID;
string address,name;
public:
data(int i,string a,string n):ID(i),address(a),name(n){}
friend class SetData;
};
class SetData
{
private:
data obj(43,"185 Awan Market","Talha"); //this is where the error happens
public:
void show()
{
cout<<"Name is: "<<obj.name<<endl;
cout<<"Address is: "<<obj.address<<endl;
cout<<"ID is: "<<obj.ID;
}
};
Run Code Online (Sandbox Code Playgroud) 我创建了一个类Point,这里是对应的hpp文件。
#ifndef POINT
#define POINT
class Point
{
protected:
int x;
int y;
public:
Point(int x = 10, int y = 10);
void movePoint(int moveX, int moveY);
void printCoordinates();
};
#endif
Run Code Online (Sandbox Code Playgroud)
我注意到在 main 中,我可以声明一个对象并以这种方式初始化它:
Point myPoint(1, 1);
Run Code Online (Sandbox Code Playgroud)
如果我想创建一个包含两个点的结构,它不会让我以这种方式初始化它,相反,我必须使用大括号,这样:
struct segment
{
Point point1 = {0, 0};
Point point2 = {15, 15};
};
Run Code Online (Sandbox Code Playgroud)
这是为什么?
我开始学习C++中的嵌套类,我尝试了一个快速代码,我粘贴在这里看看嵌套类是如何工作的.但编译以一些我无法弄清楚的错误结束.
文件:check.cpp
class Outside{
public:
class Inside{
private:
int mInside;
public:
Inside(const int& x):mInside(x){}
};
private:
Inside mOutside(20);
};
int main(void){
Outside o;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我编译的错误 g++ -Wall -std=c++11 -o check.out check.cpp
check.cpp:12:25: error: expected parameter declarator
Inside mOutside(20);
^
check.cpp:12:25: error: expected ')'
check.cpp:12:24: note: to match this '('
Inside mOutside(20);
^
Run Code Online (Sandbox Code Playgroud)
我需要在此错误背后提供一个很好的解释以及如何克服此错误.
今天当我阅读C++ Primer时,它说类内初始化程序不能使用()我在Stackoverflow上搜索并在这里找到类似的问题.并且接受的答案说:原因可能是声明之间有一个模糊的成员函数和类型成员的定义.但我不完全同意他.我尝试以下代码:
struct Sales_data
{
int i(5); //this line can't be regard as a function
};
Run Code Online (Sandbox Code Playgroud)
但是编译器仍然抱怨.谁能告诉我为什么.\编译器:clang ++版本:3-4
我希望有一个包含向量的节点数据结构.我SIZE事先知道了向量的大小,因此我使用const说明符初始化变量.
vectorTest.cpp):#include <vector>
const int SIZE = 100;
struct node {
std::vector<int> myVec(SIZE); // ERROR: 'SIZE' is not a type
};
int main(int argc, char const *argv[]) {
std::vector<int> myVec(SIZE); // VALID
return 0;
}
Run Code Online (Sandbox Code Playgroud)
g++ 5.4.0):g++ -std=c++1y vectorTest.cpp -o vectorTest
Run Code Online (Sandbox Code Playgroud)
在里面main(),一切都很好,我可以高兴地宣布:std::vector<int> A(SIZE);.但是,当我尝试在a中定义相同的内容时struct,我会收到错误消息'SIZE' is not a type.
我知道我可以这样做来int使用C风格的数组定义来声明s 的向量,
struct other_node {
int myVec[SIZE]; // VALID
};
Run Code Online (Sandbox Code Playgroud)
但我想知道为什么这是不可能的std::vector. …
c++ ×12
c++11 ×5
constructor ×2
stdvector ×2
struct ×2
vector ×2
class ×1
curly-braces ×1
declaration ×1
initializer ×1
linux ×1
stl ×1