Reputation: 11
I have searched textbooks and the web. I can't seem to find anything to explain the errors at the bottom. They are coming from the Copy constuctor and the operator= overload.
1 #ifndef SURVEYDATA_H
2 #define SURVEYDATA_H
3
4 class SurveyData
5 {
6 public:
7 int getSector();
8 int getExposure();
9 int getSpeed();
10
11 SurveyData();
12 SurveyData(int, int, int);
13 SurveyData(const SurveyData&);
14
15 SurveyData& operator=(const SurveyData&);
16 private:
17 int sector;
18 int exposure;
19 int speed;
20 };
21 #endif
Header File ^
SurveyData::SurveyData(const SurveyData& s)
18 {
19 sector = s.getSector();
20 exposure = s.getExposure();
21 speed = s.getSpeed();
22 }
23
24 SurveyData& SurveyData::operator=(const SurveyData& s)
25 {
26 sector = s.getSector();
27 exposure = s.getExposure();
28 speed = s.getSpeed();
29 return *this;
30 }
Section of .cpp file error is coming from ^
surveydata.cpp:17:43: error: definition of implicitly-declared ‘constexpr SurveyData::SurveyData(const SurveyData&)’
SurveyData::SurveyData(const SurveyData& s)
^
surveydata.cpp:24:54: error: definition of implicitly-declared ‘constexpr SurveyData& SurveyData::operator=(const SurveyData&)’
SurveyData& SurveyData::operator=(const SurveyData& s)
Errors ^ Any help understanding this is appreciated.
Upvotes: 1
Views: 725
Reputation: 104514
What the H.Krisnan said in the comment. Make those get
functions const. That's probably goodness for any place outside of your class where it makes sense to pass a SurveyData
by const reference. So go ahead and do that.
However....
You actually don't need a copy constructor nor an overloaded assignment operator in your class. Since your class doesn't have any allocated pointers or members that can't be serviced with a shallow copy, the default copy constructor and default assignment operator generated by the compiler will do nicely. So later when you add a new variable to SurveyData, you won't introduce a bug because you forgot to update the copy constructor.
Implement the copy constructor and assignment operator when a member (e.g. raw pointer) needs to be explicitly allocated for the assignment to be valid. You'd likely need a destructor as well: Rule off three. Or conversely, if your class is simple enough where it doesn't need a destructor, it probably doesn't need a copy constructor or assignment operator either to be explicitly defined.
Just take those out.
class SurveyData
{
public:
int getSector() const;
int getExposure() const;
int getSpeed() const;
SurveyData();
SurveyData(int, int, int);
private:
int sector;
int exposure;
int speed;
};
But if you really need to have a copy constructor or assignment operator, these methods have access to private data. So you can always say:
SurveyData& SurveyData::operator=(const SurveyData& s)
{
sector = s.sector;
exposure = s.exposure;
speed = s.speed;
return *this;
}
Upvotes: 2