This post has been de-listed
It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.
Hi, I wanted to welcome myself to the group and hopefully I can help out other C programmers during my time in this group. Personally, Im having a trouble with a code that inputs a file and stores it into a structure through an array.
I have the following error from the compiler using c 11
From what I understand the constructor initilization is not occuring for the last value of the array i.e. point[4]. Did I declare this right? (the same happens if I increase the array size to 5, with 5 computing no initilization of Vect2[5])
I added what I thought was necessary below
Compiler error:
main.cpp:26:9: error: no matching constructor for initialization of 'Vect2 [4]'
Vect2 points[NUM_POINTS];
^
./Vect2.hpp:20:7: note: candidate constructor (the implicit copy constructor) not viable: requires 1 argument, but 0 were provided
class Vect2 {
^
./Vect2.hpp:27:3: note: candidate constructor not viable: requires 2 arguments, but 0 were provided
Vect2(float x_, float y_);
^
My main.cpp file:
#include <fstream>
#include <iostream>
#include "Vect2.hpp"
using namespace std;
int main(int argc, char* argv[]) {
if (argc < 2) {
cout << "Please provide an input file with 5 2D vectors. Exiting.\n";
exit(1);
}
// input file stream declaration & Vect 2 declaration of array structure
ifstream input_file_stream(argv[1], ios::in);
const int NUM_POINTS = 5;
Vect2 points[NUM_POINTS];
int current_point = 0;
.....
while (!input_file_stream.eof()) {
input_file_stream >> a;
input_file_stream >> b;
points[current_point] = Vect2(a, b);
current_point ;
}
.....
My Hpp file is as follows:
class Vect2 {
private:
float x;
float y;
public:
float x_;
float y_;
Vect2(float x_, float y_);
auto getx() -> float;
auto gety() -> float;
auto setx() -> float;
auto sety() -> float;
auto distance() -> float;
~Vect2();
ostream& write(ostream& out);
};
auto distance(Vect2 x, Vect2 y) -> float;
#endif
and lastly the structure cpp file
#include "Vect2.hpp"
Vect2::Vect2(float x, float y): x_ {x}, y_{y}
// For every function, write a comment defining what the function does
// and clarify what the parameters mean.
auto vect2::getx() -> float { return x_; }
auto Vect2::gety() -> float { return y_; }
auto Vect2::setx() -> float {
*this = x;
return *this;
}
auto Vect2::sety() -> float {
*this = y;
return *this;
}
auto distance(Vect2 a, Vect2 b) -> float {
float delta_x = (a->x - b->x) * (a->x - b->x);
float delta_y = (a->y = b->y) * (a->y - b->x);
float sum = delta_x delta_y;
float d = sqrt(sum);
return d;
}
ostream& Vect2::write(ostream& out) {
out << "(" << v->x << ", " << v->y << ")";
return out;
}
So what am I missing?
Subreddit
Post Details
- Posted
- 4 years ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/cpp_questio...