Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/130.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++中函数的未定义引用_C++_Reference_Compiler Errors_Undefined - Fatal编程技术网

C++中函数的未定义引用

C++中函数的未定义引用,c++,reference,compiler-errors,undefined,C++,Reference,Compiler Errors,Undefined,我在RangeCheck、read和prntword中不断遇到错误。错误是: undefined reference to RangeCheck(short, short, short) undefined reference to read(short*, bool) undefined reference to prntword(short) 我试图改变我把函数放在main上面的位置,但我不知道如何修复错误 #include <stdio.h> #include <s

我在RangeCheck、read和prntword中不断遇到错误。错误是:

undefined reference to RangeCheck(short, short, short)

undefined reference to read(short*, bool)

undefined reference to prntword(short)
我试图改变我把函数放在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;
    }
}
试着替换

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


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

为了使用您自己的函数,您必须声明并定义它们,只有一个定义才能满足这两个要求

不知道如何修复错误

您需要定义您的功能,即提供其实现

:


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

我在main前面看到了您的函数声明,但没有看到任何函数定义,即它们是未定义的。提示:定义是函数实现存在的地方。您不定义您调用的函数。顺便说一句,根据平台的不同,调用其中一个函数可能不是一个好主意。毕竟,这是一个仅使用该名称的POSIX系统调用。如果你在C++中编程,而不是在程序中有任何C++特定代码,那你为什么要添加C标记?请不要用标签来进行垃圾邮件。这不是有效的C。并且正确地格式化代码。看看它是一个C++问答,但是它所说的很多也适用于C。
short RangeCheck(short word, short min, short max){return 1;}
char* prntword(short word){return 0;}
bool read(short *data, bool check){return 0;}
#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";
}