无法在 C++ 中的 main 中定义结构
Cant't define structure in main in c++
无法在 c++ 的 main 中定义结构。
#include <iostream>
using namespace std;
int main()
{
struct d
{
char name[20];
int age;
};
struct d s,f;
s = { "agent smith" , 17 };
cout << s.name << " is 17 year old\n";
return 0;
}
每当我编译我的代码时,我都会收到以下错误:-
$ g++ test.cpp
test.cpp: In function ‘int main()’:
test.cpp:25:27: error: no match for ‘operator=’ (operand types are ‘main()::d’ and ‘<brace-enclosed initializer list>’)
s = { "agent smith" , 17 };
^
test.cpp:18:9: note: candidate: constexpr main()::d& main()::d::operator=(const main()::d&)
struct d
^
test.cpp:18:9: note: no known conversion for argument 1 from ‘<brace-enclosed initializer list>’ to ‘const main()::d&’
test.cpp:18:9: note: candidate: constexpr main()::d& main()::d::operator=(main()::d&&)
test.cpp:18:9: note: no known conversion for argument 1 from ‘<brace-enclosed initializer list>’ to ‘main()::d&&’
我的代码有什么问题?我按照书上说的去做(c++ primer Plus 6thED)
尝试显式构造函数:
s = d{ "agent smith" , 17 };
或明确定义 initialization
d s{"agent smith", 17};
(假设C++11, so with GCC -preferably at least GCC 6- compile with g++ -std=c++11 -Wall -Wextra -g
)
PS。不要费心学习比 C++11 更早的东西。请注意,C++17 最近 approved(2017 年 9 月),但在今天还太年轻,没有成熟的实现。
我认为你的问题是你在定义变量后试图用 'initializer list' 初始化。
这应该有效:
d s = { "agent smith", 17 };
d f = { "agent john", 19 };
该程序的工作版本:
- 当你使用扩展初始化器(一个带有
{}
)时使用-std=c++11
编译选项
- 使用
->
调用结构的字段
- 使用结构后释放内存(
delete s
)
- 对字符串字段使用
string
或适当使用char
的数组("something"是一个字符串)
#include <iostream>
#include <string>
using namespace std;
struct d
{
string name;
int age;
};
int main()
{
d *s = new d{ "agent smith" , 17 };
cout << s->name << " is 17 year old\n";
delete s;
return 0;
}
无法在 c++ 的 main 中定义结构。
#include <iostream>
using namespace std;
int main()
{
struct d
{
char name[20];
int age;
};
struct d s,f;
s = { "agent smith" , 17 };
cout << s.name << " is 17 year old\n";
return 0;
}
每当我编译我的代码时,我都会收到以下错误:-
$ g++ test.cpp
test.cpp: In function ‘int main()’:
test.cpp:25:27: error: no match for ‘operator=’ (operand types are ‘main()::d’ and ‘<brace-enclosed initializer list>’)
s = { "agent smith" , 17 };
^
test.cpp:18:9: note: candidate: constexpr main()::d& main()::d::operator=(const main()::d&)
struct d
^
test.cpp:18:9: note: no known conversion for argument 1 from ‘<brace-enclosed initializer list>’ to ‘const main()::d&’
test.cpp:18:9: note: candidate: constexpr main()::d& main()::d::operator=(main()::d&&)
test.cpp:18:9: note: no known conversion for argument 1 from ‘<brace-enclosed initializer list>’ to ‘main()::d&&’
我的代码有什么问题?我按照书上说的去做(c++ primer Plus 6thED)
尝试显式构造函数:
s = d{ "agent smith" , 17 };
或明确定义 initialization
d s{"agent smith", 17};
(假设C++11, so with GCC -preferably at least GCC 6- compile with g++ -std=c++11 -Wall -Wextra -g
)
PS。不要费心学习比 C++11 更早的东西。请注意,C++17 最近 approved(2017 年 9 月),但在今天还太年轻,没有成熟的实现。
我认为你的问题是你在定义变量后试图用 'initializer list' 初始化。
这应该有效:
d s = { "agent smith", 17 };
d f = { "agent john", 19 };
该程序的工作版本:
- 当你使用扩展初始化器(一个带有
{}
)时使用-std=c++11
编译选项 - 使用
->
调用结构的字段 - 使用结构后释放内存(
delete s
) - 对字符串字段使用
string
或适当使用char
的数组("something"是一个字符串)
#include <iostream>
#include <string>
using namespace std;
struct d
{
string name;
int age;
};
int main()
{
d *s = new d{ "agent smith" , 17 };
cout << s->name << " is 17 year old\n";
delete s;
return 0;
}