Dec 28, 2020 at 11:14am UTC
Hello guys once again. I would like to ask yuou a question regarding my following code. Despite the fact that visual studio states that no issues found, when I try to compile the code it reads
<<Severity Code Description Project File Line Suppression State
Error LNK2019 unresolved external symbol "public: __thiscall box::box(void)" (??0box@@QAE@XZ) referenced in function _main>> 1
Any ideas why this?
Thank you in advance!
//Headers
#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
#include <cstdio>
using namespace std;
class box {
private:
double x{};
double y{};
double z{};
public:
box();
box(double a, double b, double c) : x(a), y(b), z(c) {};
double volume() {
return x * y * z;
};
void set_xyz(double a, double b, double c) { x = a; y = b; z = c; };
double get_xyz() { return x; return y; return z; };
friend double perimeter(box &obj);
};
double perimeter(box &obj) {
return 4 * (obj.x + obj.y + obj.z);
}
int main()
{
box obj1;
obj1.set_xyz(2, 10, 2);
cout << perimeter(obj1) << endl;
return 0;
}
Last edited on Dec 28, 2020 at 11:17am UTC
Dec 28, 2020 at 11:27am UTC
You have declared - but never defined - a constructor taking zero parameters, as you will need in main().
Change
box();
to
box(){}
and your compiler, at least, will be happy.
Alternatively, you could remove that constructor and use default arguments in your other one.
Last edited on Dec 28, 2020 at 11:27am UTC
Dec 28, 2020 at 12:18pm UTC
Guys, thank you very much for your replies. I am unacceptable by forgetting to define the constructor taking zero parameters! Thank you again.
@salem c: I know about the code tags I just forgot to set the tag. Thank you again.
Dec 28, 2020 at 12:40pm UTC
There is an issue with get_xyz(). This doesn't do what you trying to do. It will only return the value of x. To return values for x y z consider for C++17:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
#include <iostream>
#include <tuple>
using namespace std;
class box {
double x {};
double y {};
double z {};
public :
box() noexcept =default ;
box(double a, double b, double c) noexcept : x(a), y(b), z(c) {}
double volume() const noexcept { return x * y * z; };
void set_xyz(double a, double b, double c) noexcept { x = a; y = b; z = c; };
auto get_xyz() const noexcept { return tuple(x, y, z); }
friend double perimeter(const box& obj) noexcept ;
};
double perimeter(const box& obj) noexcept {
return 4 * (obj.x + obj.y + obj.z);
}
int main()
{
box obj1;
obj1.set_xyz(2, 10, 2);
cout << perimeter(obj1) << '\n' ;
const auto [x, y, z] {obj1.get_xyz()};
cout << "x = " << x << " y = " << y << " z = " << z << '\n' ;
}
Last edited on Dec 28, 2020 at 3:24pm UTC