【问题标题】:Undefined reference to functions in C++ [duplicate]C ++中对函数的未定义引用[重复]
【发布时间】:2017-01-22 21:29:11
【问题描述】:

我不断收到RangeCheckreadprntword 的错误。错误是:

undefined reference to RangeCheck(short, short, short)

undefined reference to read(short*, bool)

undefined reference to prntword(short)

我尝试更改放置函数的位置(在 main 上方,在 main 中),但我不知道如何修复错误。

#include <stdio.h>
#include <stdlib.h>

#define READ  10
#define WRITE 11
#define LOAD  20
#define STORE 21
#define ADD   30
#define SUBTRACT 31
#define DIVIDE 32
#define MULTIPLY 33
#define BRANCH 40
#define BRANCHNEG 41
#define BRANCHZERO 42
#define HALT  43
#define CELLS 100
#define RANGE 9999
#define SENTINEL -1

#define DEBUG 0

short RangeCheck(short word, short min, short max);
char* prntword(short word);
bool read(short *data, bool check);

int main()
{

    bool error = false;
    char *word, OperationCode, Operand;
    short memory[CELLS], InstructionRegister;
    int counter, Accumulator;
    Accumulator = 0;

    for (int i = 0; i < CELLS; i++) {
        memory[i] = 0;
    }

    for (counter = 0; !error; counter++); {
        counter = RangeCheck(counter, 0, CELLS - 1);
        InstructionRegister = memory[counter];
        OperationCode = InstructionRegister / 100;
        Operand = InstructionRegister % 100;
    } 

    switch(OperationCode) {
        case READ:
            read(&memory[Operand], false);
            break;
        case WRITE:
            printf("%s\n", word = prntword(memory[Operand]));
            break;
        case LOAD:
            Accumulator = memory[Operand];
            break;
        case STORE:
            memory[Operand] = RangeCheck(Accumulator, -RANGE, RANGE);
            break;
        case ADD:
            Accumulator += memory[Operand];
            break;
        case SUBTRACT:
            Accumulator -= memory[Operand];
            break; 
        case DIVIDE:
            Accumulator /= memory[Operand];
            break;
        case MULTIPLY:
            Accumulator *= memory[Operand];
            break;
        case BRANCH:
            break;
    }
}

【问题讨论】:

  • 我在main 之前看到了您的函数声明,但我没有看到任何函数定义(即它们未定义)。提示:定义是函数的实现所在。
  • 你没有定义你调用的函数。
  • 顺便说一下,根据平台的不同,调用您的某个函数read 可能是个坏主意。毕竟,它是一个仅具有该名称的 POSIX 系统调用。如果你用 C++ 编程(不是说你的程序中有任何特定于 C++ 的代码),那么你为什么要添加 C 标签?请不要使用标签发送垃圾邮件。
  • 这不是有效的 C。并正确格式化代码。
  • 看看What is an undefined reference/unresolved external symbol error and how do I fix it? 这是一个 C++ 问题和答案,但它所说的很多内容也适用于 C。

标签: c++ reference compiler-errors undefined


【解决方案1】:

为了使用您自己的函数,您必须声明和定义它们(单独的定义可以满足两者)。

不知道如何修复错误

您需要定义您的函数(即提供其实现)。

Example:

#include <iostream>

void foo(); // My function declaration

int main()
{
    foo(); // To use this function it must be declared and defined

    return 0;
}

// The function definition
void foo()
{
    std::cout << "foo\n";
}

【讨论】:

    【解决方案2】:

    尝试替换

    short RangeCheck(short word, short min, short max);
    char* prntword(short word);
    bool read(short *data, bool check);
    

    short RangeCheck(short word, short min, short max){return 1;}
    char* prntword(short word){return 0;}
    bool read(short *data, bool check){return 0;}
    

    它应该会为你编译(但是它可能不会像你期望的那样工作)

    【讨论】:

      【解决方案3】:

      您已声明并引用了这 3 个函数但未定义。您需要提供所有这 3 个功能的实现。

      【讨论】:

        猜你喜欢
        • 2014-04-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-30
        • 1970-01-01
        • 2012-05-24
        • 2019-04-12
        相关资源
        最近更新 更多