-1

I am quite new to c++ and I really don't understand what this error is or why it is occuring. I am receiving no useful information on why it is not compiling except for some unreferenced things. I have followed tutorials online for this and am getting errors nobody else has when doing this? Maybe I shouldn't use the tutorials I have found, have you got any?

Anyway the error is as follows:

main.o -> main.cpp:(.text+0x16e): undefined reference to 'validate::set_string(std::string)'
collect2.exe -> [Error] Id returned 1 exit status
Makefile.win -> recipe for target 'Math++.exe' failed

The code is as follows:

#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

string getName();
string name;

class validate {
    public:
        string item;
        void set_string (string);
        bool vInput() {

        }
        bool vName() {
            cout << item;
            system("pause");
            return true;
        }
};

int main() {
    name = getName();
}

string getName() {
    validate val;
    cout << "Enter your name: ";
    cin >> name;
    val.set_string(name);
    bool result = val.vName();
    return name;
}

I have tried moving the "validate val;" around to main, outside all functions and nothing happened with that. I also tried removing the vInput function as it has no return but that also made no difference.

After a comment that told me to declare the functions, I tried doing so but still got the compilation error. I declared them like this which may be incorrect for classes:

bool vName();
bool vInput();
void set_string (string);

The code after people have tried to help me:

#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

string getName();
string name;

bool vName();
bool vInput();
void set_string (string);

class validate {
    public:
        string item;
        void set_string (string);
        bool vInput() {
            return true;
        }
        bool vName() {
            cout << item;
            system("pause");
            return true;
        }
};

int main() {
    name = getName();

}

string getName() {
    validate val;
    cout << "Enter your name: ";
    cin >> name;
    val.set_string(name);
    bool result = val.vName();
    return name;
}

Nobody seems to understand why my set_string does nothing. Well its not suppose to! I am following an example on how to do this and they dont do any function?

// classes example
#include <iostream>
using namespace std;

class Rectangle {
    int width, height;
    public:
        void set_values (int,int);
        int area() {return width*height;}
 };

void Rectangle::set_values (int x, int y) {
    width = x;
    height = y;
}

int main () {
    Rectangle rect;
    rect.set_values (3,4);
    cout << "area: " << rect.area();
    return 0;
}
Reece
  • 23
  • 6
  • 4
    You declared a function `void set_string(string)` but didn't define it. – Andrew Cheong Apr 07 '16 at 14:37
  • I didn't realise functions in classes needed to be defined. And the compiler should see that it is there before it reaches any calls to it? – Reece Apr 07 '16 at 14:39
  • You have `val.set_string(name);` in `getName()`. What will the compiler do there if there is no `set_string` function defined? – 001 Apr 07 '16 at 14:44
  • @JohnnyMopp I have these declared outside of getName() – Reece Apr 07 '16 at 14:46
  • At the start of the program to be exact like you would usually do. – Reece Apr 07 '16 at 14:47
  • @Reece, this is great, but where's the _definition_? :) – ForceBru Apr 07 '16 at 14:47
  • Nobody has any clue what `set_string` is supposed to do. – SergeyA Apr 07 '16 at 14:49
  • Regarding your last edit: __nope__, you have to _define_ this function now. The _declaration_ is OK. BTW, those declarations you've made in your edit are incorrect, remove them. – ForceBru Apr 07 '16 at 14:49
  • @Reece Where is the function body of `validate::set_string()`? It doesn't exist in the code you've given us. – 001 Apr 07 '16 at 14:50
  • I was told to do this from this tutorial – Reece Apr 07 '16 at 14:51
  • I can't help but recall my first experience with programming. I was sixth-grader, and was doing my first ever basic program with the help of someone. I did multiplication, and happily multiplied two numbers. There were no errors, yet program didn't print the result. I asked, very confused, 'where is the result'? And the guy replied - 'but you never asked for the result to be printed', and I asked - 'didn't it understand that I need to know the result of this'? And he replied - 'computers understand nothing'. Morale: computer doesn't understand what your set_string has to do. – SergeyA Apr 07 '16 at 14:52
  • http://www.cplusplus.com/doc/tutorial/classes/ They declare an int before assigning it but it has no function? – Reece Apr 07 '16 at 14:52
  • @Reece, dude, what don't you understand? Suppose you declare `int test(int a, int b);` and then call `int k = test(5,7);`. The question is: __who knows what `test` does???__ – ForceBru Apr 07 '16 at 14:53
  • 1
    @ForceBru I feel like OP somehow believes that name `set_string` should hint the compiler on what needs to be done. Hence my story above. – SergeyA Apr 07 '16 at 14:54

2 Answers2

1

You declared a function called set_string, so the compiler now knows that there exists a function called like this. This means that if it encounters a call to this function, it knows that it's valid. The problem is, you never defined it.

Here's basic logic: a function must do something, right? Here you don't even have a function, so how should the compiler guess what it does? That's why it generates no object code for it. Then the linker comes and sees that lack of object code and raises an error.

ForceBru
  • 43,482
  • 10
  • 63
  • 98
  • I understand this basic logic, I know what the it means. But why am I getting this error when I have both declared my functions and they have things to do. I told you that when I remove vInput as it returns nothing, the program is still broken. – Reece Apr 07 '16 at 14:44
  • 1
    @Reece, the problem is with `set_string`, __not__ `vInput` – ForceBru Apr 07 '16 at 14:46
1

As far as the compiler is concerned, it's not an error not to define the body of a method that you have declared within a class. That's what allows us to put possibly very complex method bodies in a different source file, which is often advantageous to writing clearer code. But the linker will object if it can't find the body for a method that you call.

Somewhere in your program, not necessarily in the same source file, you should have something like

void validate::set_string(string parameter)
{
 // your code goes here
}
Logicrat
  • 4,438
  • 16
  • 22
  • So how can I allow it to find the body for the method? – Reece Apr 07 '16 at 14:45
  • More standard-aligned terminology is needed before this answer can be upvoted. – SergeyA Apr 07 '16 at 14:46
  • @SergeyA I am open to suggestions. I agree with your comment but am not up to speed on the precise wording of the standards. – Logicrat Apr 07 '16 at 14:47
  • @Logicrat, I suggest start with difference between *definition* and *declaration*, and not only with regards to functions, but variables too. This might proove an exercise youseful for you! – SergeyA Apr 07 '16 at 14:48
  • At the point in your program where you do define the body, the compiler should have access to the class definition of `validate`. If you put it in the same source file, that's fine, but if you use a separate source file, then the class definition for `validate` should be in a `.h` file that is included in each source file where `validate` is used (or defined). – Logicrat Apr 07 '16 at 14:49