0

I am trying to create a class which calls one of it's functions when created, but I am getting the following error when compiling:

g++ -std=c++11 -Wall -Wextra -Werror -pedantic-errors -DNDEBUG -c src/PuzzleSolution.cpp
src/PuzzleSolution.cpp:7:32: error: definition of implicitly-declared 'PuzzleSolution::PuzzleSolution()'
 PuzzleSolution::PuzzleSolution()
                                ^
src/PuzzleSolution.cpp:12:6: error: prototype for 'void PuzzleSolution::addRow()' does not match any in class 'PuzzleSolution'
 void PuzzleSolution::addRow()
      ^
src/PuzzleSolution.h:19:10: error: candidate is: void PuzzleSolution::addRow(std::vector<unsigned int>&)
     explicit PuzzleSolution();
          ^
src/PuzzleSolution.cpp:17:48: error: no 'void PuzzleSolution::addElement(unsigned int)' member function declared in class 'PuzzleSolution'
 void PuzzleSolution::addElement(unsigned int id)
                                                ^
make: *** [PuzzleSolution.o] Error 1

Here is the header:

#include <vector>

using namespace std;

class PuzzleSolution {
private:
    vector<vector<unsigned int>> sol;

public:
    explicit PuzzleSolution();

    void addRow();
};

Here is the cpp file:

#include "PuzzleSolution.h"

PuzzleSolution::PuzzleSolution()
{
    addRow();
}

void PuzzleSolution::addRow()
{
    this->sol.emplace_back();
}

What am I doing wrong?

Tomer Amir
  • 1,515
  • 4
  • 27
  • 54
  • 1
    No idea. [Your code builds as is on GCC 7.2](http://coliru.stacked-crooked.com/a/c0b3282021c70176) – StoryTeller - Unslander Monica Nov 21 '17 at 11:59
  • I am using g++-4.8.2 I think. I have to build and make it work on a specific machine @StoryTeller – Tomer Amir Nov 21 '17 at 12:00
  • 2
    You seem to be compiling with another header defining `PuzzleSolution`. The errors make no sense with the header you've given here. – Quentin Nov 21 '17 at 12:02
  • [It builds on GCC 4.8.4 too](https://wandbox.org/permlink/lVizXOHMnzst7hiJ) – StoryTeller - Unslander Monica Nov 21 '17 at 12:02
  • I agree @ArkadiuszKoćma, but it's not my choice – Tomer Amir Nov 21 '17 at 12:02
  • @Quentin could it be an issue with the compiler I am using? it's a university server which I have no control over... – Tomer Amir Nov 21 '17 at 12:05
  • Actually, there's one difference between the code I used to test and yours. I dropped the `using namespace std;`. Probably not realted, but give it a go. – StoryTeller - Unslander Monica Nov 21 '17 at 12:06
  • 2
    @TomerAmir make sure that you saved your changes to `PuzzleSolution.h` and that there are no duplicates of this file lying around. – Quentin Nov 21 '17 at 12:06
  • Found the issue... I don't know why, but there were two files in the same folder called: `PuzzleSolution.h.gch` and `PuzzleSolution.h~`. Once I removed them, the issue got fixed... @Quentin – Tomer Amir Nov 21 '17 at 12:09
  • 3
    @TomerAmir alright, `PuzzleSolution.h~` is typically a backup file from your text editor and is harmless. `PuzzleSolution.h.gch`, however, is a [precompiled header file](https://stackoverflow.com/questions/1241399/what-is-a-h-gch-file), which was used instead of the "vanilla" header you were editig. Precompiled headers are neat, but you have to remember to rebuild them when changes are made :) – Quentin Nov 21 '17 at 12:12
  • Thanks Quentin and StroyTeller! I'll update my answer – Tomer Amir Nov 21 '17 at 12:14

2 Answers2

0

The code as it is has no error. It compiles with GCC 4.8.2

Be sure that your header file is indeed what you have linked to. Most likely the header being included is different than the one you have actually posted here.

Side Note: Generally it is considered as a bad practice to put using namespace std; in a header file.

iBug
  • 35,554
  • 7
  • 89
  • 134
OriBS
  • 722
  • 5
  • 9
0

Found the issue:

There was a file in the src folder called PuzzleSolution.h.gch

@Quatin and @StoryTeller helped me to understand that this is a pre-compiled header, which the compiler kept using.

Once deleted, the project compiled and executed

Tomer Amir
  • 1,515
  • 4
  • 27
  • 54