【问题标题】:Unresolved external symbol referenced in mainmain 中引用的未解析的外部符号
【发布时间】:2019-11-12 16:18:27
【问题描述】:

我必须创建一个程序,它有一个函数 charster(),它接受三个参数:一个字符和两个整数。它将每 y 行打印字符输入 x 次。我收到语法错误 LNK2019 和 LNK1120 在 main.xml 中引用的未解析的外部符号。并且 IDK 我做了什么导致这个或做什么来纠正它。

这是我的代码:

#include <stdio.h>

void charster(char n, int n_char, int n_lines);

int main(void)
{
    char n;
    int n_lines, n_chars;

    printf("Please enter a character to be printed, the amount of times it should appear, and the amount of lines that should appear: ");

    while (scanf("%c, %d, %d", &n, &n_chars, &n_lines) == 3)
    {
        charster(n, n_chars, n_lines);
    }
    return 0;


    void charster(char n, int n_char, int n_lines);
    {
        int count; 

        for (count = 0; count < n_lines; count++)
        {
            putchar(n);
        }
    }
}

【问题讨论】:

  • 此代码格式错误。 C 不支持嵌套函数定义。
  • 所以我应该把函数移到 main 上面?
  • 是的。或以下,因为你有一个合适的原型。
  • 不,这个函数在main的里面。在main 的关闭} 之前。
  • 我已经缩进了代码以使这一点更明显。如果您认为该功能是分开的,我想这只是一个错字。

标签: c visual-studio function syntax


【解决方案1】:

首先,C 语言不支持嵌套函数。所以你必须在主函数之外编写函数charster()

你可以写在主函数的上面或下面,因为原型已经声明了。

请看下面的代码:

#include<stdio.h>

void charster(char n, int n_char, int n_lines);

int main(void)
{
    char n;
    int n_lines, n_chars;

    printf("Please enter a character to be printed, the amount of times it should appear, and the amount of lines that should appear: ");

    while (scanf("%c %d %d", &n, &n_chars, &n_lines) == 3)
    {
        charster(n, n_chars, n_lines);
    }
    return 0;
}

void charster(char n, int n_char, int n_lines)
{
    int times; 
    int lines;
    for (lines = 0; lines < n_lines; lines++)
    {
        for(times=0;times<n_char;times++){
            printf("%c ", n);
        }
        printf("\n");
    }
}

【讨论】:

  • 我更正了,现在我在文件范围内找到了一个 C2449(缺少函数头)
  • 好吧,我猜这只是错误,我能够编译它。但是它不会产生任何输出。
  • @Eysaak 正在研究它。再给我几秒钟
  • @Eysaak,请检查我是否更正了 scanf() 语法并在逻辑上更正了代码。如果这能解决你的问题,那就太好了。如果您满意,请在正确的地方标记。
  • 尝试输入 c 2 3
猜你喜欢
  • 2013-12-18
  • 1970-01-01
  • 2010-09-30
  • 1970-01-01
  • 1970-01-01
  • 2013-09-13
  • 2011-02-09
相关资源
最近更新 更多