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;
}