C++中的错误信息:”was not declared in this scope”。

huangapple go评论199阅读模式
英文:

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: &#39;cars&#39; was not declared in this scope
   13 |         cin &gt;&gt; cars[i];
      |                ^~~~
project2.cpp:17:17: error: &#39;cars&#39; was not declared in this scope
   17 |         cout &lt;&lt; cars[i];
#include &lt;iostream&gt;
#include &lt;string&gt;
void display(){
    int amount;
    cout &lt;&lt; &quot;Car amount: &quot;;
    cin &gt;&gt; amount;
    string cars[amount];
    for(int i=0; i&lt;amount; i++){
        cout &lt;&lt; &quot;Enter &quot; &lt;&lt; i &lt;&lt; &quot; car name:&quot;;
        cin &gt;&gt; cars[i];
        &#39;\n&#39;;
    }
    for(int i=0; i&lt;amount; i++){
        cout &lt;&lt; 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];
    }

}

因此,例如coutcinstring等名称找不到。至少应该将使用指令放在函数定义之前。

请注意,作为函数内部使用的数组

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 &lt;&lt; &quot;Car amount: &quot;;
    cin &gt;&gt; amount;
    string cars[amount];
    for(int i=0; i&lt;amount; i++){
        cout &lt;&lt; &quot;Enter &quot; &lt;&lt; i &lt;&lt; &quot; car name:&quot;;
        cin &gt;&gt; cars[i];
        &#39;\n&#39;;
    }
    for(int i=0; i&lt;amount; i++){
        cout &lt;&lt; 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&lt;std::string&gt; like

std::vector&lt;std::string&gt; cars( amount );

To use the container you need to include header &lt;vector&gt;.

Also this expression statement

&#39;\n&#39;;

has no effect. Remove it.

Instead you could write for example

for(int i=0; i&lt;amount; i++){
    cout &lt;&lt; &quot;Enter &quot; &lt;&lt; i &lt;&lt; &quot; car name:&quot;;
    cin &gt;&gt; cars[i];
}

cout &lt;&lt; &#39;\n&#39;;

for(int i=0; i&lt;amount; i++){
    cout &lt;&lt; cars[i] &lt;&lt; &#39;\n&#39;;
}

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 &lt;&lt; &quot;Car amount: &quot;;

and so on.

答案2

得分: 1

    //新代码:
    #include <iostream>
    #include <string>
    #define amount 5 //&lt;---- 定义数组中的汽车数量
    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 &lt;iostream&gt;
#include &lt;string&gt;
#define amount 5 //&lt;---- defines the number of cars in the array
using namespace std;

void display() 
{
    //int amount;
    //cout &lt;&lt; &quot;Car amount: &quot;;
    //cin &gt;&gt; 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 &lt; amount; i++) 
    {
        cout &lt;&lt; &quot;Enter &quot; &lt;&lt; i + 1 &lt;&lt; &quot; car name:&quot; &lt;&lt; endl; //added endl instead of \n
        //                      ^more undestandable as it starts from 1 instead of 0
        cin &gt;&gt; cars[i];
        //&#39;\n&#39;; doesnt work like this, it need to be used in a string as single character, like &quot;\ncars&quot;
    }
    for (int i = 0; i &lt; amount; i++) 
    {
        cout &lt;&lt; cars[i] &lt;&lt; &quot; &quot;; //more understandable
    }

}

int main()
{
    display();
    return 0;
}

huangapple
  • 本文由 发表于 2023年6月16日 02:40:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76484616.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定