1

I have the following problem already longer time. The point ist that I have read a bit on stackoverflow and I have used a typedef, but I doesnt help me.

Looking forward to get some help :)

Align_vector.h :

#include <utils/vector_2d.h>
class Vector_2d;

namespace Utils {

class Align_vector : public Vector_2d {
protected:
    bool check_range(int x, int y);

public:

    typedef enum {left, right, up, down} Alignment;

    Align_vector(Alignment alignment);
    void set_alignment(Alignment alignment);
    Alignment get_alignment();

};

} /* namespace Utils */

Align_vector.cc :

#include <utils/align_vector.h>


namespace Utils {

Align_vector::Align_vector(Alignment alignment) {
    this->alignment = alignment;
}

void set_alignment(Alignment alignment) {
    Align_vector::alignment = alignment;
}

Alignment get_alignment() {
    return Align_vector::alignment ;
}

bool check_range(int x, int y) {
    switch ( Align_vector::alignment ) {
        case Align_vector::left:
            if (x == -1 && y == 0) {
                return true;
            } else {
                return false;
            }
            break;
        case Align_vector::right:
            if (x == 1 && y == 0) {
                return true;
            } else {
                return false;
            }
            break;
        case Align_vector::down:
            if (x == 0 && y == -1) {
                return true;
            } else {
                return false;
            }
            break;
        case Align_vector::up:
            if (x == 0 && y == 1) {
                return true;
            } else {
                return false;
            }
            break;
        default:
            return false;
            break;
    }

}


} /* namespace Utils */

Here is the error:

/utils/align_vector.cc:14:47: error: no matching function for call to ‘Utils::Vector_2d::Vector_2d()’ Align_vector::Align_vector(Alignment alignment) {

kellerprogger
  • 109
  • 2
  • 2
  • 12
  • `typedef enum {...} Alignment;` looks a bit strange to me. Where did you get this from? Why not `enum Alignment {...}` ? – 463035818_is_not_an_ai Jul 04 '16 at 11:37
  • In a forum they said, that this would be a better solution, but even with you solution I am getting the same error. – kellerprogger Jul 04 '16 at 11:39
  • better solution to what? Btw there are other things in the code that arent clear. What is `Align_vector::alignment` supposed to be? – 463035818_is_not_an_ai Jul 04 '16 at 11:40
  • Did you define `Vector_2d` in `utils/vector_2d.h`? If yes, why do you re-declare it here (`class Vector_2d;`)? What the compiler tells you is that you have no default constructor for `Vector_2d`. – Holt Jul 04 '16 at 11:40
  • I am only defining Align_vetor2d, but where do i define vector2d? – kellerprogger Jul 04 '16 at 11:42
  • @kellerprogger It's your base class. It's most likely defined in "utils/vector_2d.h". (Your syntax suggests that one of [these](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) would be good for you.) – molbdnilo Jul 04 '16 at 11:51

1 Answers1

2

This code has numerous issues. First of all, you have defined an enum called Alignment, but you did not declare a member of that type. To do so, add this line after the definition of the enum:

Alignment alignment;

Your definitions of methods are also incorrect, the alignment is supposed to belong to a specific object, while you are using it in several functions as if it were a static member of the class. Here are the fixed versions of method definitions:

namespace Utils {

Align_vector::Align_vector(Alignment alignment) {
    this->alignment = alignment;
}

void Align_vector::set_alignment(Alignment alignment) {
    this->alignment = alignment;
}

Align_vector::Alignment Align_vector::get_alignment() {
    return this->alignment;
}

bool Align_vector::check_range(int x, int y) {
    switch(this->alignment) {
    case Align_vector::left:
        if(x == -1 && y == 0) {
            return true;
        }
        else {
            return false;
        }
        break;
    case Align_vector::right:
        if(x == 1 && y == 0) {
            return true;
        }
        else {
            return false;
        }
        break;
    case Align_vector::down:
        if(x == 0 && y == -1) {
            return true;
        }
        else {
            return false;
        }
        break;
    case Align_vector::up:
        if(x == 0 && y == 1) {
            return true;
        }
        else {
            return false;
        }
        break;
    default:
        return false;
        break;
    }

}


} /* namespace Utils */

Finally, you are missing a definition of a default constructor for base class Vector_2d (your comments suggest that you did not define that class at all, you just declared its existance using statement class Vector_2d;). Judging by your overall implementation, I think user @molbdnilo was correct when suggesting you should learn more about C++ programming in general.

Hope this helps!

Marko Popovic
  • 3,999
  • 3
  • 22
  • 37
  • Now I understand, I thought I only need a normal constructor of vector2d, now I added the default one and everything works, thank you very much! – kellerprogger Jul 04 '16 at 12:13