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

C++中Triangle类构造函数传参异常及无参构造属性值一致问题求助

Fixing Triangle Object Creation Issues in C++

Let’s walk through the problems you’re facing and get your Triangle class working correctly. From what you’ve described, there are two main culprits here: incorrect constructor implementation (leading to identical objects when using parameterized construction) and uninitialized member variables from the default no-arg constructor.

1. Why Your Parameterized Constructor Creates Identical Objects

The most common reasons for this behavior are:

  • Name collision between parameters and member variables: If your constructor parameters share the same name as your class members (like using m_ID as a parameter), assigning m_ID = m_ID just sets the variable to itself, completely ignoring the input value.
  • Accidentally marking member variables as static: Static members are shared across all instances of the class, so every Triangle object would inherit the same values for those variables.

Fix for Parameterized Constructor

First, confirm none of your member variables are static (unless you intentionally want shared state across all objects). Then, use either initialization lists (the most efficient and clean approach) or this-> to resolve name conflicts. Here’s a corrected implementation:

class Triangle {
private:
    // Non-static members: each object gets its own unique copy
    int m_ID;
    int m_S1, m_S2, m_S3;
    int m_T1, m_T2, m_T3;

public:
    // Parameterized constructor using initialization list
    Triangle(int id, int s1, int s2, int s3, int t1, int t2, int t3)
        : m_ID(id),
          m_S1(s1), m_S2(s2), m_S3(s3),
          m_T1(t1), m_T2(t2), m_T3(t3) {}

    // Add getters to verify object values (optional but helpful)
    int getID() const { return m_ID; }
    int getS1() const { return m_S1; }
    // ... repeat getters for other members
};

If you must use matching names for parameters and members, use this-> to explicitly reference the class member:

Triangle(int m_ID, int m_S1, int m_S2, int m_S3, int m_T1, int m_T2, int m_T3) {
    this->m_ID = m_ID;
    this->m_S1 = m_S1;
    // ... assign remaining members
}

When creating objects, double-check you’re passing unique values for each instance:

Triangle triangle1(1, 0, 1, 2, 3, 4, 5);
Triangle triangle2(2, 1, 2, 3, 4, 5, 6); // Unique ID and vertex/neighbor values
Triangle triangle3(3, 2, 3, 4, 5, 6, 7);

2. Fixing the No-Arg Constructor Issue

By default, if you don’t define any constructors, C++ generates a default no-arg constructor—but this constructor does not initialize built-in types like int. This leaves your member variables with random garbage values, and if you’re seeing identical values across objects, it’s just coincidental memory garbage, not intentional behavior.

Options for the No-Arg Constructor

  • Explicitly initialize all members: If you need to create empty Triangle objects, define a no-arg constructor that sets sensible default values:
    Triangle()
        : m_ID(-1), // Use -1 to signal an uninitialized/empty triangle
          m_S1(-1), m_S2(-1), m_S3(-1),
          m_T1(-1), m_T2(-1), m_T3(-1) {}
    
  • Delete the no-arg constructor: If you never need to create Triangle objects without parameters, delete the default constructor to prevent accidental uninitialized objects:
    Triangle() = delete;
    

Final Quick Checks

  • Double-check that none of your member variables are marked static unless you specifically want shared state across all Triangle instances.
  • Use the getter methods you added to confirm each object holds the correct values after creation.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 10:07:19