从“char”转换为转换为非标量类型“Vector”抛出错误未知原因

发布于 2024-10-08 06:34:26 字数 435 浏览 0 评论 0原文

抛出未知错误

#include "std_lib_facilities.h"

int main()  
{  
    vector<char> shape = ('a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a');  
    return(0);  
}

带有向量的一行抛出此错误的原因是我以前从未见过也无法弄清楚...

:: g++ TicTacToe.cpp -o TTT
TicTacToe.cpp:在函数int main()'中:
TicTacToe.cpp:5: 错误:请求从
char' 到非标量类型“Vector”的转换 请

参阅每个井字游戏框中,它将以 _ 开头,然后转到 X 或 O,I'我在没有图形库的情况下执行此操作,因此它是终端图形。

Error Thrown Unknown Reason

#include "std_lib_facilities.h"

int main()  
{  
    vector<char> shape = ('a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a');  
    return(0);  
}

that one line with the vector throws this error that I've never seen before nor can figure out...

:: g++ TicTacToe.cpp -o TTT
TicTacToe.cpp: In function int main()':
TicTacToe.cpp:5: error: conversion from
char' to non-scalar type `Vector' requested

See in each tic-tac-toe box it will start as _ and then go to either X or O, I'm doing this without a graphics library so its terminal graphics.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

野心澎湃 2024-10-15 06:34:26

如果所有值都相同,您可以使用 std::vector 的以下构造函数:

std::vector<char> shape( 9, 'a' );

如果部分或所有值不同,您可以使用另一个构造函数,如下所示:

static const char ini[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i' };
std::vector<char> shape( ini, ini+sizeof(ini) );

或者

static const char ini[] = "abcdefghi";
std::vector<char> shape( ini, ini+sizeof(ini)-1 );

If all value are the same you can use the following constructor of the std::vector:

std::vector<char> shape( 9, 'a' );

If part or all values are distinct you can use another constructor as follows:

static const char ini[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i' };
std::vector<char> shape( ini, ini+sizeof(ini) );

Or

static const char ini[] = "abcdefghi";
std::vector<char> shape( ini, ini+sizeof(ini)-1 );
黯淡〆 2024-10-15 06:34:26

这种向量初始化在当前的 C++ 中是不可能的。您可以使用 :

vector<char> shape(9, 'a');

来达到所需的效果(创建一个大小为 9 的向量,其中所有元素都设置为 '_')。

如果 boost 是一个选项,Boost.Assign 提供了您最初想要实现的语法类型,尽管在这种特殊情况下使用适当的 std:: vector 构造函数基本上就足够了。

Such vector initialization isn't possible in current C++. You can use :

vector<char> shape(9, 'a');

which will have the desired effect (create a vector of size 9 where all elements are set to '_').

If boost is an option, Boost.Assign provides the kind of syntax you wanted to achieve initially, although in this particular case using the appropriate std::vector constructor is largely sufficient.

一枫情书 2024-10-15 06:34:26

Vector 没有这样的构造函数。请参阅有效的构造函数

vector doesn't have such a constructor. See the valid constructors.

微暖i 2024-10-15 06:34:26

vector 类具有以下构造函数:

explicit vector( size_type n, const T& value= T(), const Allocator&=Allocator());

使用它您可以执行以下操作:

vector<char> shape (9,'a');

The vector class has the following constructor:

explicit vector( size_type n, const T& value= T(), const Allocator&=Allocator());

Using which you can do:

vector<char> shape (9,'a');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文