0

I'm having some difficulty reproducing an example program of Object-Oriented Programming Using C++ described in "Encapsulation and Type Extensibility."

For simplicity's sake, I've cut out most of the code to focus on the specific error at hand:

#include <iostream> // Access standard IO library
#include <string> //Access type 'string'

using namespace std; //Use standard library namespace

const int max_length = 255;

class my_string {
      public:
             void assign(const char* st);
             int length() const { return len; }
             void print() const
                  { cout << s << "\nLength: " << len << endl; }
      private:
              char s[max_length];
              int len;
              };      

int main()
{
    my_string one;
    one.assign("I'm sorry Dave, I'm afraid I can't do that.");
    one.print();
    system("PAUSE");
}

When I try to compile, I get the error message:

[Linker error] undefined reference to 'my_string::assign(char const*)'

I'm not sure what I'm doing wrong. My best guess is that assign is incorrectly defined, since the main() block seems fine.

Edit:

The complete example as written in the book is:

In file string1.cpp

const int max_len = 255;

class my_string {
      public:
             void assign(const char* st);
             int length() const { return len; }
             void print() const
                  { cout << s << "\nLength: " << len << endl; }
      private:
              char s[max_length];
              int len;
              };      

int main()
{
    my_string one, two;
    char three[40] = {"My name is Charles Babbage."};

    one.assign("My name is Alan Turing.");
    two.assign(three);
    cout << three;
    cout << "\nLength: " << strlen(three) << endl;
    if (one.length() <= two.length())
        one.print();
    else
        two.print();
}






R. Burton
  • 111
  • 6
  • 3
    You declare `assign` but not defining it. – Eraklon Mar 22 '20 at 16:17
  • Did you forget to post the code for `assign()`? Or is there no code for assign()? Or did you implement it outside the class and forgot you need to prefix it with `my_string::` ? if it is implemented outside the class it should be `void my_string::void assign(const char* st) { // Implementation here }` – drescherjm Mar 22 '20 at 16:19
  • @drescherjm This is taken directly from the example in the book. Granted, the book is old (1997), so there might be some discrepancy. The book doesn't include anything else in the `my_string` block. – R. Burton Mar 22 '20 at 16:20
  • Maybe they want you to come up with the implementation. – drescherjm Mar 22 '20 at 16:21
  • @drescherjm That might be a problem. This is literally the first time I've ever used C++. – R. Burton Mar 22 '20 at 16:22
  • 2
    This might help: https://stackoverflow.com/q/12573816/9254539 – eesiraed Mar 22 '20 at 16:33
  • fwiw you would have gotten the same error back in 1997, so the book being old isnt the issue here – 463035818_is_not_an_ai Mar 22 '20 at 16:47

2 Answers2

1

The compiler can't find it's definition.

Usually there is a header file (.h) where the class' declaration is put, including as less as possible and a source file (.cpp) that includes all the definitions.

  • The header file declarations tells the compiler which methods shall be available (as a promise),
  • the source file should contain the definition of the functions that are declared in the header file.

If they aren't defined, meaning there is no body for that function, it can't be executed. In your book, the code is both declared and defined, by writing the methods inside the class' definition.

You could do the same:

public:
    void assign(const char* st) {
        /* implementations of the assign method here
        (or leave it empty for this example, but rather don't)*/
    };
    int length() const { return len; };
    ...
  • The book uses a single file, but I think I understand what you're saying. – R. Burton Mar 22 '20 at 16:26
  • "defined" is the word you are looking for. The method is declared but not defined in OPs code. Btw seperating into header and source would yield the same error. Doest really matter wehre the method is defined as long as it is defined – 463035818_is_not_an_ai Mar 22 '20 at 16:50
1

Linking and compiling errors are two different things. A compiler error means that you did something wrong in the syntax.

A linking error tells you that there is a part missing when the linker tries to put your program together.

[Linker error] undefined reference to 'my_string::assign(char const*)'

This error tells you that somewhere the promise was made to the compiler that my_string::assign(char const*) exists and can be used (by a declaration void assign(const char* st);). But in the linking step the linker cannot find that function.

If the error references a function that you have written, then you might have forgotten the definition of it or have mismatching signature between declaration and definition.

t.niese
  • 39,256
  • 9
  • 74
  • 101