使用带有函数的struct类型

时间:2017-09-21 12:04:27

标签: c++

Main.cpp的

#include "Header.h"
SolveSE(1, 2, 3);

Header.h

struct Solution;
Solution SolveSE(double ax, double bx, double c);

SSE.cpp

#include "Header.h"
struct Solution
{
    size_t count;
    double *roots;
};

Solution SolveSE(double ax, double bx, double c) 
{   
    if (fabs(ax)<1e-5)
    {
        throw std::invalid_argument("a should not be a zero");
    }
    double Discriminant = bx - 4 * ax * c;

    if (Discriminant > 0)
    {
        double x1 = -bx + sqrt(Discriminant) / 2 * ax;
        double x2 = -bx - sqrt(Discriminant) / 2 * ax;
        double roots[] = { x1, x2 };
        return { 2, roots };
    }
    if (Discriminant == 0)
    {
        double x1 = -bx + sqrt(Discriminant) / 2 * ax;
        double roots[] = { x1};
        return { 1, roots };
    }
    if (Discriminant < 0)
    {
        return { 0};
    }
    return {};
}

Visual Studio出错:

严重级代码描述项目文件行抑制状态 错误C2027使用未定义类型'解决方案'SolveSquareEquation c:\ users \ dima \ documents \ visual studio 2017 \ projects \ solvesquareequation \ solvesquareequation \ main.cpp 8

浮动提示说,返回类型'解决方案'是不完整的。

我的功能实现有什么用?

TEST.CPP

TEST_METHOD(TestSSE)
{
    Assert::AreEqual<Solution>(SolveSE(1,3,-4), {2, {4, 1}})
}

1 个答案:

答案 0 :(得分:1)

你应该将struct移动到头文件:

struct Solution
{
    size_t count;
    double *roots;
};