英文:
C++ "was not declared in this scope"
问题
嗨,基本上我正在编写这个简单的函数,询问你有多少辆车,将数量输入到数组中,并将车辆的名称分配给数组。我还用一个循环来显示它,并且它显示在范围内未声明,我就是搞不明白为什么会这样说。有人能帮我吗?
错误:project2.cpp:13:16: 错误:'cars' 在此作用域中未声明
13 | cin >> cars[i];
| ^~~~
project2.cpp:17:17: 错误:'cars' 在此作用域中未声明
17 | cout << cars[i];
#include <iostream>
#include <string>
void display(){
int amount;
cout << "汽车数量:";
cin >> amount;
string cars[amount];
for(int i=0; i<amount; i++){
cout << "输入第" << i << "辆车的名称:";
cin >> cars[i];
'\n';
}
for(int i=0; i<amount; i++){
cout << cars[i];
}
}
using namespace std;
int main()
{
display();
return 0;
}
英文:
Heyo, basically I was writing this simple function to ask you how many cars you have, inputing the amount in the array, and assigning the names of the cars to the array aswell. Also made a for loop to make display it and it says it was not declared in the scope and I just can't figure it out why it says this. Can someone help me out?
error: project2.cpp:13:16: error: 'cars' was not declared in this scope
13 | cin >> cars[i];
| ^~~~
project2.cpp:17:17: error: 'cars' was not declared in this scope
17 | cout << cars[i];
#include <iostream>
#include <string>
void display(){
int amount;
cout << "Car amount: ";
cin >> amount;
string cars[amount];
for(int i=0; i<amount; i++){
cout << "Enter " << i << " car name:";
cin >> cars[i];
'\n';
}
for(int i=0; i<amount; i++){
cout << cars[i];
}
}
using namespace std;
int main()
{
display();
return 0;
}
答案1
得分: 1
问题在于使用指令放在main
函数之前。
using namespace std;
int main()
{
display();
return 0;
}
在这里,没有使用来自std
命名空间的名称。另一方面,在放置使用指令之前,您在display
函数内部使用了来自标准命名空间std
的名称。
void display(){
int amount;
cout << "Car amount: ";
cin >> amount;
string cars[amount];
for(int i=0; i<amount; i++){
cout << "Enter " << i << " car name:";
cin >> cars[i];
'\n';
}
for(int i=0; i<amount; i++){
cout << cars[i];
}
}
因此,例如cout
、cin
和string
等名称找不到。至少应该将使用指令放在函数定义之前。
请注意,作为函数内部使用的数组
string cars[amount];
不是标准的C++特性。而是应该使用容器std::vector<std::string>
,如下所示:
std::vector<std::string> cars( amount );
要使用这个容器,您需要包含头文件<vector>
。
此外,这个表达式语句
'\n';
没有效果。请删除它。
取而代之,您可以这样写,例如:
for(int i=0; i<amount; i++){
cout << "Enter " << i << " car name:";
cin >> cars[i];
}
cout << '\n';
for(int i=0; i<amount; i++){
cout << cars[i] << '\n';
}
总的来说,在全局命名空间中使用使用指令是一个不好的主意。更好的做法是使用限定名称,例如:
std::cout << "Car amount: ";
等等。
英文:
The problem is that the using directive is placed before main
using namespace std;
int main()
{
display();
return 0;
}
where neither name from the namespace std
is used. On the other hand, you are using names from the standard namespace std
within the function display
that is placed before the using directive.
void display(){
int amount;
cout << "Car amount: ";
cin >> amount;
string cars[amount];
for(int i=0; i<amount; i++){
cout << "Enter " << i << " car name:";
cin >> cars[i];
'\n';
}
for(int i=0; i<amount; i++){
cout << cars[i];
}
}
So names as for example cout
, cin
and string
are not found. At least you should place the using directive before the function definition.
Pay attention to that variable length arrays as the array used within the function
string cars[amount];
are not a standard C++ feature.
Instead use container std::vector<std::string>
like
std::vector<std::string> cars( amount );
To use the container you need to include header <vector>
.
Also this expression statement
'\n';
has no effect. Remove it.
Instead you could write for example
for(int i=0; i<amount; i++){
cout << "Enter " << i << " car name:";
cin >> cars[i];
}
cout << '\n';
for(int i=0; i<amount; i++){
cout << cars[i] << '\n';
}
In general it is a bad idea to use the using directive in the global namespace. It is much better to use qualified names like for example
std::cout << "Car amount: ";
and so on.
答案2
得分: 1
//新代码:
#include <iostream>
#include <string>
#define amount 5 //<---- 定义数组中的汽车数量
using namespace std;
void display()
{
string cars[amount]; //现在,您已经定义了一定数量的汽车,C++不允许将变量用作数组的长度
for (int i = 0; i < amount; i++)
{
cout << "输入第 " << i + 1 << " 辆汽车的名称:" << endl; //添加 endl 代替 \n
cin >> cars[i];
}
for (int i = 0; i < amount; i++)
{
cout << cars[i] << " "; //更易理解
}
}
int main()
{
display();
return 0;
}
英文:
//new code:
#include <iostream>
#include <string>
#define amount 5 //<---- defines the number of cars in the array
using namespace std;
void display()
{
//int amount;
//cout << "Car amount: ";
//cin >> amount;
string cars[amount]; //now, you have a defined number of cars, c++ doesnt allow to use a variables as lenght of array
for (int i = 0; i < amount; i++)
{
cout << "Enter " << i + 1 << " car name:" << endl; //added endl instead of \n
// ^more undestandable as it starts from 1 instead of 0
cin >> cars[i];
//'\n'; doesnt work like this, it need to be used in a string as single character, like "\ncars"
}
for (int i = 0; i < amount; i++)
{
cout << cars[i] << " "; //more understandable
}
}
int main()
{
display();
return 0;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论