-4

I keep getting an error with RangeCheck, read, prntword. The errors are:

undefined reference to RangeCheck(short, short, short)

undefined reference to read(short*, bool)

undefined reference to prntword(short)

I tried to change where I placed the functions (above main, in main), but I've no idea what to do to fix the error.

#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;
    }
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Nolan Bradshaw
  • 464
  • 6
  • 14
  • 3
    I see your **function declarations** before `main` but I do not see any **function definitions** (i.e., they are undefined). Hint: The definition is where the implementation of the function exists. – James Adkison Jan 22 '17 at 21:32
  • You don't define the functions you call. –  Jan 22 '17 at 21:33
  • By the way, depending on platform it might be a bad idea to call one of your functions `read`. It is, after all, a POSIX system call with just that name. And if you're programming in C++ (not that there is any C++-specific code in your program), then why did you add the C tag? Please don't spam with tags. – Some programmer dude Jan 22 '17 at 21:35
  • That's not valid C. And format the code properly. – too honest for this site Jan 22 '17 at 21:45
  • Take a look at [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) It is a C++ question and answer, but a lot of what it says applies to C too. – Jonathan Leffler Jan 22 '17 at 21:46
  • Incidentally, note the superfluous semicolon in `for (counter = 0; !error; counter++); { counter = RangeCheck(counter, 0, CELLS - 1);` — the one that is the empty body of the for loop and the statement block that follows the loop. GCC 6.3.0 pointed it out in no uncertain terms: ```ur23.c:41:5: error: this ‘for’ clause does not guard... [-Werror=misleading-indentation] for (counter = 0; !error; counter++); { ^~~ ur23.c:41:43: note: ...this statement, but the latter is misleadingly indented as if it is guarded by the ‘fo’ for (counter = 0; !error; counter++); {```. – Jonathan Leffler Jan 22 '17 at 21:52
  • @Olaf: the only major sin as C code was the absence of ``; otherwise, the code is basically C code. I agree the title has a reference to CPP which is misleading; the issue is not with the C PreProcessor, and it might be intended to be C++, but then why was the question tagged C? 'Tis mysterious — carelessness or confusion on the part of the OP, no doubt. – Jonathan Leffler Jan 22 '17 at 21:56
  • @JonathanLeffler Probably because so many think `c` and `c++` are the *same thing*... – James Adkison Jan 22 '17 at 21:58
  • @JamesAdkison: It may be, and the confusion isn't helped by the Microsoft C development environment, I think. – Jonathan Leffler Jan 22 '17 at 22:00
  • @JonathanLeffler: According to the history, the question was originally tagged C++ **and** C. Reading the "CPP" in the title as C++ instead of the more common "C PrePreocessor" and considering the lack of `stdbool.h`, it sounds evident OP compiles as C++. I agree this is C-**style**, though and OP should make up his mind. Until then, I'd say it should be read as bad C++. (and when has some MS tool ever been helpful? ;-) – too honest for this site Jan 22 '17 at 23:45
  • @Olaf: … and, I suppose the type-safe linkage errors (like `undefined reference to read(short*, bool)`) are indicative that a C++ compiler/linker is in use. It means that the 'what is an undefined reference' question is a perfectly good 'duplicate'. – Jonathan Leffler Jan 22 '17 at 23:48

3 Answers3

1

In order to use your own functions you must declare and define them (a definition alone can satisfy both).

no idea what to do to fix the error

You need to define your function (i.e., provide its implementation).

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";
}
James Adkison
  • 9,412
  • 2
  • 29
  • 43
0

Try to replace

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

with

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

and it should compile for you (however it may not work as you expect)

Soren
  • 14,402
  • 4
  • 41
  • 67
0

You have declared and referenced these 3 functions but not defined. You need to provide implementation of all these 3 functions.

VHS
  • 9,534
  • 3
  • 19
  • 43