C++ 类类型上函数中的重载 #包括 #包括 #包括 #包括“lineType.h” 使用名称空间std; int main() { 双x,y; 双a=1。; 双b=0。; 双c=1。; 双d=2。; 双e=0。; 双f=3。; 双g=0。; 双h=4。; 双i=-1。; 线型line1(a、b、c); 线型line2(d、e、f); 线型line3(g、h、i); cout

C++ 类类型上函数中的重载 #包括 #包括 #包括 #包括“lineType.h” 使用名称空间std; int main() { 双x,y; 双a=1。; 双b=0。; 双c=1。; 双d=2。; 双e=0。; 双f=3。; 双g=0。; 双h=4。; 双i=-1。; 线型line1(a、b、c); 线型line2(d、e、f); 线型line3(g、h、i); cout,c++,header-files,C++,Header Files,lineType::lineType是隐式生成的构造函数,因为您没有提供任何用户定义的构造函数。默认生成的构造函数不带参数,但您尝试在行中提供三个参数: #include "lineType.h" #include <iostream> #include <iomanip> #include <cmath> using namespace std; void lineType::display() const { cout << a &

lineType::lineType
是隐式生成的构造函数,因为您没有提供任何用户定义的构造函数。默认生成的构造函数不带参数,但您尝试在行中提供三个参数:

#include "lineType.h"
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

void lineType::display() const
{
    cout << a << "x + " << b << "y = " << c << endl;
}

bool lineType::isParallel(const lineType& line) const
{
    if (a == 0 && line.a == 0)
        return 1;
    if (b == 0 && line.b == 0)
        return 1;
    else if (-a / b == -line.a / line.b)
        return 1;
    else
        return 0;
}

bool lineType::isPerp(const lineType& line) const
{
    if (a == 0 && line.b == 0)
        return 1;
    if (b == 0 && line.a == 0)
        return 1;
    else if (-a / b == line.b / line.a)
        return 1;
    else
        return 0;
}

bool lineType::intersect(const lineType& line, double& x, double& y)
{
    if (a == 0)
        x = c / b;
    if (line.a == 0)
        x = line.c / line.b;
    if (b == 0)
        y = c / a;
    if (line.b == 0)
    {
        y = line.c / line.a;
    }
    else
    {
        x = ((a*line.c) - (c*line.a)) / ((b*line.a) - (a*line.b));
        y = ((c*line.b) - (b*line.c)) / ((b*line.a) - (a*line.b));
    }
    if (a == 0 && line.a == 0)
        return 0;
    if (b == 0 && line.b == 0)
        return 0;
    return 1;
}

lineType::lineType()
{
    a = 0;
    b = 0;
    c = 0;
}

lineType::lineType(double a2, double b2, double c2)
{
    a = a2;
    b = b2;
    c = c2;
}


lineType::~lineType()
{
}
我怀疑您想利用它,但不幸的是,您不能使用它,因为您的
a
b
c
变量都是
私有的
。您可能想自己添加这样的构造函数:

lineType line1(a, b, c);
lineType line2(d, e, f);
lineType line3(g, h, i);
但这还不是全部。您的代码还有几个问题。值得注意的是:

lineType(const double a, const double b, const double c)
        :a(a), b(b), c(c) { }

不会编译,因为
intersect()
返回
void
,而不是
bool
如果
语句要求提供
bool
s或可以隐式转换为
bool
类型的内容,则返回
bool
。使函数返回
bool
,这是函数名以
is
开头的逻辑假设

您似乎没有采用3个参数的
lineType
构造函数。您只有这一个:
lineType();
我刚创建了一个采用3个参数的构造函数,但它仍然不接受。这是我在头文件lineType(双a2、双b2、双c2)的代码中添加的内容;我确实解决了您建议的问题,除了线型构造函数我添加了一个线型(双a2、双b2、双c2);同时,它也等于类中的私有信息,但仍然会遇到相同的错误,然后发布完整的错误消息并指出它所指的位置。您可能还希望共享
.cpp
文件我刚刚发布了lineType.cpp文件和错误消息。我非常感谢您的帮助,因为它帮助修复了大部分错误如果我丢失了任何东西,我很抱歉。“DreKLangar我复制了你的代码到我的IDE,它编译并运行得很好。不能复制错误。你确定你包含了所有的东西吗?”DreKal愤怒考虑编译和运行不同IDE /不同编译器上的代码,看看你是否能在那里重现错误。可能是一个错误W。项目结构或IDE/编译器中的错误
lineType line1(a, b, c);
lineType line2(d, e, f);
lineType line3(g, h, i);
lineType(const double a, const double b, const double c)
        :a(a), b(b), c(c) { }
if (line1.isParallel(line2)) cout << "line1 is parallel to line 2" << endl;
if (line2.intersect(line3, x, y))