函数参数中未声明和未定义的标识符

Undeclared and undefined identifier's in the parameter of a function

本文关键字:未定义 标识符 未声明 参数 函数      更新时间:2023-10-16
  #include <stdio.h>
  #include <iostream>
  using namespace std;
  void instructions();
int menu();
void drawt(int& s, char& c);

int main()
{
int choice;
instructions();
choice = menu();
if (choice != 1 && choice != 2)
{
    cout << "You requested to quit. Bye! n";
    return 0;
} 
if (choice = 1) {
    drawt(s, c);
}
//draw the shape the user requested
//draw_shape(choice);
system("pause");
return 0;
}
void instructions() {
cout << "This program will create a triangle or diamond of your choice in 
size!n";
cout << "The number you enter will either be the size of the bottom of the 
triangle or the size of the middle of the diamond!n";
}
int menu() {
int c;
cout << "Please choose between drawing a triangle, diamond or exiting the 
program!n";
cout << "Enter 1 for a triangle,2 for a diamond and 3 to exit!n";
cin >> c;
   return c;
   }
   void drawt(int& s, char& c){
  cout << "enter base size of trianglen";
  cin >> s;
   cout << "Now enter the character you wish the triangle to be made of!n";
   cin >> c;
   int length = 1;
    for (int i= 0; i < 5; i++) {
       for (int j = 0; j < s; j++) {
        cout << c;
    }
    cout << endl;
    length++;
    if (length = s) {
        break;
        }
      }
   }

它说s和c都是未定义的,没有确定的,我不知道为什么它不起作用。我只是学会了功能,我不确定我是正确地称其为正确还是正确地使用了标题或确切的内容做错了。我抬头看了很多教程等。该程序应该画出一个三角形或钻石的人,然后以任何人的选择,然后从他们选择的特征中绘制出来。它还没有完成,但是我现在如此挂断。谢谢!

当您从main()调用此功能时:

drawt(s, c);

您将2个变量S和C发送到该函数。但是这些变量在main()中还不存在 - 这是这里的汇编问题。您需要首先在main()中声明它们:

int s;
char c;

应该解决您的基本问题。

您应该尝试更多的功能练习。您在定义drawt()的方式上存在一些问题(并非关键) - 它使用的是可以在本地定义的参数,即使main()从未使用过它们,也可以将其作为参考接收。这些是您应该学习的重要事情,但是它们不会像现在这样打破您的程序。

另外,您应该立即开始始终如一 - 非常重要:)