0

I am trying to pass an object of Pair class into the constructor of the FollowsQuery class as shown below. I am getting a no default constructor exists error. I am not sure how to rectify this error.

This is the definition of the Pair class below in Pair.h. The object is created via a static method CreatePair.

class Pair { public: pair<DesignEntities, string> firstPair; pair<DesignEntities, string> secondPair; static Pair CreatePair(string first, string second, map<string, DesignEntities> varMap); pair<int, int> getStatementAndPos(); pair<DesignEntities, DesignEntities> getTypes(); private: Pair(pair<DesignEntities, string> first, pair<DesignEntities, string> second); }; 

This is my follows query class definition file. FollowsQuery.h

class FollowsQuery : Query { public: FollowsQuery(Pair pair); ~FollowsQuery(); Pair pair; // for (s1,s2) in follows(s1,s2) list<int> filterList; void setList(list<int> filteredList); // set the curr list from the prev select list<string> evaluate(PKB pkb); bool isRelatedToSelect(DesignEntities designEntity); // check if design entity was from prev select }; 

This is my FollowsQuery.c class file, where the error occurs.

FollowsQuery::FollowsQuery(Pair pair) { this->pair = pair; } 

Even though I have looked through stackoverflow and found similar questions, I have been unable to find the answer because I want to create the Pair class separately and pass it into the constructor of FollowsQuery.

6
  • You use Pair correctly in the definition (implementation) of the FollowsQuery constructor, but not in the declaration of the constructor. What are the differences between the two? Commented Jan 31, 2020 at 14:01
  • I have tried this constructor but i dont get it still. FollowsQuery::FollowsQuery(Pair pair(std::pair<DesignEntities, string>, std::pair<DesignEntities, string>)) { this->pair = pair; } Commented Jan 31, 2020 at 14:04
  • I said that the definition (the implementation) used it correctly, you should copy that way into the declaration in the class instead of the opposite. Pair pair is the correct way to declare a variable (including arguments). All your other functions declare their arguments correctly, it's only the declaration of the FollowsQuery constructor that you do it wrong. I don't want to insult you, but perhaps you should take some time to refresh some basic C++? Commented Jan 31, 2020 at 14:07
  • I have changed the declaration in the header file to FollowsQuery(Pair pair), but the .cpp file is still telling me that no default constructor exists. Commented Jan 31, 2020 at 14:10
  • 1
    Oh, that's a different error. When asking questions about build error, please always include the full and complete error output in the question, copy-pasted as text. It's kind of hard to see what error you're asking about, especially since the FollowsQuery constructor declaration stood out with its error. Commented Jan 31, 2020 at 14:11

1 Answer 1

2

You get the "no default constructor exist" for the pair member of the FollowsQuery class, because the compiler tries to default-construct pair but the Pair class doesn't have a default constructor.

What happens in the FollowsQuery construction is basically this:

FollowsQuery::FollowsQuery(Pair pair) // Here the `FollowsQuery::pair` member is default constructed { // Here you assign to the already constructed `FollowsQuery::pair` member this->pair = pair; } 

As the Pair class doesn't have a default constructor (as mentioned) then this will not work.

You solve this by using a constructor initializer list to initialize (construct) the member variables:

FollowsQuery::FollowsQuery(Pair pair) : pair(pair) // Copy-construct the `FollowsQuery::pair` member { // Nothing needed here, the `FollowsQuery::pair` member is already initialized } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.