Конструкторы по умолчанию и перегруженные конструкторы в файлах заголовка / реализации C ++? - PullRequest
0 голосов
/ 11 мая 2018

Я работаю над домашним заданием на C ++. Я зациклен на получении конструктора по умолчанию и перегруженного конструктора для работы в файлах заголовка / реализации. Я продолжаю получать сообщение об «ожидаемом первичном выражении до» («токен», и stackoverflow и google fu пока не помогли.

Main:

#include <iostream>
#include "Triangle.h"
#include "triangle.cpp"

using namespace std;

int main()
{
    Triangle first;

    // show that default is properly built
    cout << "Default is 3, 4 5 triangle, should be scalene and right" << endl;
    cout << " for default a is " << first.getA() << " b is " << first.getB() << " c is " << first.getC() << endl;
    cout << " default " << (first.isEquilateral() ? "is" : "is not") << " equalateral" << endl;
    cout << " default " << (first.isScalene() ? "is" : "is not") << " scalene" << endl;
    cout << " default " << (first.isIsosceles() ? "is" : "is not") << " isosceles" << endl;
    cout << " default " << (first.isRight() ? "is" : "is not") << " right" << endl << endl;

    // test setter methods
    first.setA(2);
    first.setB(2);
    first.setC(3);
    cout << "Modified is 2, 2, 3 triangle, should be isosceles" << endl;
    cout << " for modified a is " << first.getA() << " b is " << first.getB() << " c is " << first.getC() << endl;
    cout << " modified " << (first.isEquilateral() ? "is" : "is not") << " equalateral" << endl;
    cout << " modified " << (first.isScalene() ? "is" : "is not") << " scalene" << endl;
    cout << " modified " << (first.isIsosceles() ? "is" : "is not") << " isosceles" << endl;
    cout << " modified " << (first.isRight() ? "is" : "is not") << " right" << endl << endl;

    // test overloaded constructor
    Triangle second(4,4,4);
    cout << "Second is 4,4,4 triangle, should be equalateral and isosceles" << endl;
    cout << " for second a is " << second.getA() << " b is " << second.getB() << " c is " << second.getC() << endl;
    cout << " second " << (second.isEquilateral() ? "is" : "is not") << " equalateral" << endl;
    cout << " second " << (second.isScalene() ? "is" : "is not") << " scalene" << endl;
    cout << " second " << (second.isIsosceles() ? "is" : "is not") << " isosceles" << endl;
    cout << " second " << (second.isRight() ? "is" : "is not") << " right" << endl << endl;

    return 0;
}

Заголовок:

#ifndef TRIANGLE_H
#define TRIANGLE_H


class Triangle
{
    public:
        Triangle();
        Triangle(int a, int b, int c);
        // accessor methods for a, b, c.
        int getA();

        int getB();

        int getC();
        // mutator methods for a, b, c.
        void setA(int x);

        void setB(int y);

        void setC(int z);
        // equilateral triangles have sides of the same length.
        bool isEquilateral();
        // scalene triangles have unequal sides.
        bool isScalene();
        // isoceles triangles have 2 equal sides.
        bool isIsosceles();
        // right triangles use pythagorean theorem a^2 + b^2 = C^2. the other derivation will also word depending
        // on how sides are declared.
        bool isRight();
        // variables set as private so that only the members of Triangle can modify
    private:

        int a, b, c;
};

#endif // TRIANGLE_H

Triangle.cpp:

#include "Triangle.h"
#include <iostream>

using namespace std;

Triangle::Triangle()
{
    // constructor setting sides a, b, c.
        Triangle();{
            a = 3;
            b = 4;
            c = 5;
        }
}

Triangle::Triangle(int a, int b, int c)
{
    Triangle(int a, int b, int c){
            setA(a);
            setB(b);
            setC(c);
        }
}
// accessor methods for a, b, c.
int Triangle::getA() {
        return a;
    }

int Triangle::getB() {
        return b;
    }

int Triangle::getC() {
        return c;
    }
// mutator methods for a, b, c.
void Triangle::setA(int x) {
        a = x;
    }

void Triangle::setB(int y) {
        b = y;
    }

void Triangle::setC(int z) {
        c = z;
    }
// equilateral triangles have sides of the same length.
bool Triangle::isEquilateral() {
    if (a == b && b == c && a == c) {
        return true;
    }

    else {
        return false;
    }
}
// scalene triangles have unequal sides.
bool Triangle::isScalene() {
    if (a != b && b != c && a != c) {
        return true;
    }

    else {
        return false;
    }
}
// isoceles triangles have 2 equal sides.
bool Triangle::isIsosceles() {
    if (a == b && a != c) {
        return true;
    }

    else if (a == c && a != b) {
        return true;
    }

    else if (b == c && b != a) {
        return true;
    }

    else {
        return false;
    }
}
// right triangles use pythagorean theorem a^2 + b^2 = C^2. the other derivation will also word depending
// on how sides are declared.
bool Triangle::isRight() {
    if (((a * a) + (b * b)) == (c * c)) {
        return true;
    }
    else if (((a * a) + (c * c)) == (b * b)) {
        return true;
    }
    else if (((b * b) + (c * c)) == (a * a)) {
        return true;
    }
    else {
        return false;
    }
}

При выполнении кода я продолжаю получать сообщение об «ожидаемом первичном выражении до» (ошибка «токен». Я не могу пройти мимо этого - любая помощь приветствуется.

1 Ответ

0 голосов
/ 11 мая 2018

Я не уверен, где вы взяли этот синтаксис для определения конструкторов.

Для конструктора по умолчанию вы можете использовать:

Triangle::Triangle()
{
   a = 3;
   b = 4;
   c = 5;
}

Еще лучше, делегируйте его для использования другогоКонструктор.

Triangle::Triangle() : Triangle(3, 4, 6) {}

Другой конструктор можно исправить и упростить с помощью:

Triangle::Triangle(int a, int b, int c) : a(a), b(b), c(c) {}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...