相关疑难解决方法(0)

C++ 11初始化列表失败 - 但仅限于长度为2的列表

我找到了一个不起眼的日志记录错误,事实上长度为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++ initializer-list c++11

71
推荐指数
2
解决办法
2933
查看次数

为什么类内初始值设定项只能使用=或{}?

类内初始值设定项(C++ 11特性)必须用花括号括起来或跟在=符号后面.它们可能未在括号内指定.

这是什么原因?

c++ initializer curly-braces c++11

23
推荐指数
1
解决办法
1058
查看次数

字符串常量前的预期标识符

有这样的程序:

#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++ linux constructor

10
推荐指数
1
解决办法
3万
查看次数

最烦恼的解析防止在类中初始化std :: vector <int>

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例子,这没有问题?

c++ constructor initialization most-vexing-parse c++11

10
推荐指数
1
解决办法
258
查看次数

矢量声明"预期参数声明器"

我在类的私有成员变量中有一行代码:

vector<double> dQdt(3)
Run Code Online (Sandbox Code Playgroud)

在xcode中编译时,会出现"预期参数声明符"错误.我想我提供了足够的信息.我没有看到这个声明有什么问题.

c++ vector

4
推荐指数
5
解决办法
1万
查看次数

为什么支撑或相等的初始化器支撑或相等?

#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)

使用括号初始值设定项似乎是一个错误.这背后的目的是什么?

c++

3
推荐指数
1
解决办法
1991
查看次数

为什么这会说“预期”(“成本为整数?”)

例如:

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)

有机会可以帮我一下吗?

c++ class declaration stdvector

3
推荐指数
1
解决办法
131
查看次数

C++表示数字常量之前的预期标识符

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)

c++

2
推荐指数
1
解决办法
1万
查看次数

为什么结构内部的对象初始化不同?

我创建了一个类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++ struct initialization member-initialization c++11

2
推荐指数
1
解决办法
64
查看次数

嵌套类.error:期望的参数声明符 - 用于内部类实例

我开始学习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++ member-initialization c++11

1
推荐指数
1
解决办法
4522
查看次数

为什么类内初始值设定项不能使用()

今天当我阅读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

c++

0
推荐指数
1
解决办法
87
查看次数

在结构中声明向量时,C++不是类型错误

我希望有一个包含向量的节点数据结构.我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)

COMPILE(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++ struct stl vector stdvector

0
推荐指数
1
解决办法
817
查看次数