You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++默认与重载构造函数头/实现文件报错求助:expected primary expression错误

解决C++构造函数编译错误:expected primary expression before '(' token

嘿,我看了你的代码,一下子就找到了几个导致那个expected primary expression before '(' token编译错误的问题,咱们来一一解决:

核心问题分析

1. 构造函数实现语法完全错误

你在Triangle.cpp里的构造函数写法犯了基础错误:

  • 默认构造函数里,你错误地写了Triangle();{ a = 3; b = 4; c = 5; },这相当于在构造函数体内又声明了一个构造函数,编译器完全无法理解这种写法,正确的做法是直接写函数体内容。
  • 重载构造函数里,你重复写了Triangle(int a, int b, int c){...},这属于在函数内部嵌套函数定义,这在C++里是不允许的,直接实现函数逻辑即可。

2. isRight()函数缺失闭合大括号

看你Triangle.cpp的最后,isRight()函数的else分支结束后没有加},导致代码结构不完整,编译器会报错。

3. 主函数错误包含了.cpp文件

你在main函数里写了#include "triangle.cpp",这会导致类的成员函数被重复定义,C++中只需要包含头文件.h,.cpp文件是用来单独编译的,不需要在其他文件中包含。

修正后的完整代码

修正后的Triangle.cpp

#include "Triangle.h"
#include <iostream>
using namespace std;

// 修正后的默认构造函数
Triangle::Triangle() {
    a = 3;
    b = 4;
    c = 5;
}

// 修正后的重载构造函数
Triangle::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;
    }
} // 补上缺失的闭合大括号

修正后的主函数代码

#include <iostream>
#include "Triangle.h"
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;
}

把这些代码替换你原来的文件后,编译错误应该就能解决了,运行后也能得到你预期的输出结果。

内容的提问来源于stack exchange,提问作者Grimhammer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 07:24:37