例如,我不能这样写:
class A
{
vector<int> v(12, 1);
};
Run Code Online (Sandbox Code Playgroud)
我只能写这个:
class A
{
vector<int> v1{ 12, 1 };
vector<int> v2 = vector<int>(12, 1);
};
Run Code Online (Sandbox Code Playgroud)
对C++ 11语言设计的差异有何考虑?
当使用std :: vector的fill构造函数(任一形式)和C++ 11的类成员初始化功能时,以下代码无法编译(在clang/llvm 3.6下):
#include <vector>
class Foo
{
std::vector<char> buf_(10); //compiler error!
std::vector<std::vector<char>> buf2_(10, std::vector<char>(20)); //compiler error!
public:
void Bar();
};
void Foo::Bar()
{
std::vector<char> buf3_(10); //OK
std::vector<std::vector<char>> buf4_(10, std::vector<char>(20)); //OK
}
Run Code Online (Sandbox Code Playgroud)
我已经搜索了有关向量填充构造函数和类成员初始化的问题,但是已经空了.知道我错过了什么吗?
在这个完整的代码中:
class foo
{
public:
foo(const int pin);
};
class bar {
public:
// Constructor
bar(const int dataPin) : dataPin_ (dataPin) { }
private:
const int dataPin_;
foo myFoo_ (dataPin_); // instance of foo
};
int main (void)
{
return 0;
}
Run Code Online (Sandbox Code Playgroud)
使用g ++ 4.8.4我收到错误:
g++ -Wall -c "test.cpp" (in directory: /home/nick/Development)
test.cpp:14:17: error: ‘dataPin_’ is not a type
foo myFoo_ (dataPin_); // instance of foo
^
Compilation failed.
Run Code Online (Sandbox Code Playgroud)
使用clang 3.4-1ubuntu3我得到:
test.cpp:14:17: error: unknown type name 'dataPin_'
foo myFoo_ (dataPin_); …
Run Code Online (Sandbox Code Playgroud) 我有一个HashTable <Customer>作为另一个类的成员.
HashTable <T>的构造函数采用int值来确定HashTable数组的大小.
HashTable(int numItems) { ... } //constructor
Run Code Online (Sandbox Code Playgroud)
以下声明
HashTable<Customer> customers(10000); //doesn't call constructor???
Run Code Online (Sandbox Code Playgroud)
收到10000下面的"预期的类型说明符"错误.当我删除10000时,我收到错误"找不到客户的函数定义".这让我相信编译器将我的对象声明视为函数声明.
当我使用动态分配声明我的HashTable时,
HashTable<Customer> * customers = new HashTable<Customer>(10000); //works
Run Code Online (Sandbox Code Playgroud)
与编译器没有混淆.
为什么动态分配工作,而不是其他?
编辑:这是一个具有上述相同问题的最小代码.
#ifndef _BUSINESS_LOGIC
#define _BUSINESS_LOGIC
#include "HashTable.h"
class BusinessLogic
{
public:
BusinessLogic();
~BusinessLogic();
void start();
private:
HashTable<int> * custom = new HashTable<int>(10000); //works
HashTable<int> customers(10000); //error
};
#endif
#ifndef _HASH_TABLE
#define _HASH_TABLE
template<class T>
class HashTable
{
public:
HashTable(int numItems) {
if (numItems <= 0) {
throw std::invalid_argument("Invalid HashTable size");
}
currItems = …
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
我确信我误解了一些事情.
在类中实例化一个struct对象并将其作为构造函数中的值传递后,我得到一个错误?
错误:'test'不是一种类型
#include <iostream>
using namespace std;
struct Test
{
int x = 11;
int y = 22;
};
class A
{
private:
int foo = 100;
public:
A(Test tmp){}
};
class B
{
private:
Test test;
A a(test); //error
public:
B(){}
};
int main()
{
B b;
return 0;
}
Run Code Online (Sandbox Code Playgroud)