我在使用这个程序时遇到了麻烦。只有1个编译错误

时间:2017-03-16 06:11:15

标签: c++ visual-c++ bitmap

添加函数pyth.p_t时遇到问题。该错误在第40行。该行正在向函数pyth.p_t添加100。用户输入值,以便不确定功能。

ConsolApplication3.cpp:

#include "stdafx.h"
#include "main_to_pyth.h"

#include <direct.h>
#include <math.h>
#include "bitmap_image.hpp"

#include <cstdio>
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main(){
    float a, b, c;
    float x1, x2, x3;
    float y1, y2, y3;

    ifstream myfile;

    bitmap_image image("Pythagorean_Theorom.bmp");
    image_drawer draw(image);


    cout << "Enter a,b,c of pythagorean theorom and 0 \n for the value you want to find." << endl;
    cout << "Enter Value 'a' \n";
    cin >> a;
    cout << "Enter Value 'b' \n";
    cin >> b;
    cout << "Enter Value 'c' \n";
    cin >> c;
    pyth.p_t(a, b, c);
    //consol output
    if (a == 0) {
        cout << "a = " << pyth.p_t;

        float x1 = 200; //a
        float y1 = 100 + pyth.p_t; //a

        float x2 = 200 + b; //b
        float y2 = 100; //b

        float x3 = 200; //c
        float y3 = 100 - c; //c
    }
    else if (b == 0) {
        cout << "b = " << pyth.p_t;
    }
    else if (c == 0) {
        cout << "c = " << pyth.p_t;
    }
    else {
        cout << "You failed... i am disapointed";
    }
    //consol output

        draw.pen_color(255, 255, 255);
        draw.triangle( x1, y1, x2, y2, x3, y3);

    image.save_image("Pythagorean_Theorom.bmp");

    myfile.open("Pythagorean_Theorom.bmp");

    system("pause");
    return 0;
}

Pythagorean Theorom.cpp:

#include "stdafx.h"

#include <math.h>
#include "main_to_pyth.h"

#include <iostream>
#include <string>

using namespace std;

    static float p_t(float a, float b, float c) {

        if (a == 0 && b != 0 && c != 0) {
            // 0 equals nothing for parameters
            //a
            float pyth_c = (pow(c, 2)) - (pow(b, 2));
            float pyth_a = sqrt(pyth_c);
            return pyth_a;
        }
        else if (a != 0 && b == 0 && c != 0) {
            //b
            float pyth_c_1 = (pow(c, 2)) - (pow(a, 2));
            float pyth_b_1 = sqrt(pyth_c_1);
            return pyth_b_1;
        }
        else if (a != 0 && b != 0 && c == 0) {
            //c
            float pyth_a_2 = (pow(a, 2)) - (pow(b, 2));
            float pyth_c_2 = sqrt(pyth_a_2);
            return pyth_c_2;
        }
        else {
            return 0;
        }
    }

main_to_pyth.h:

#pragma once

#ifndef MAIN_TO_PYTH_H
#define MAIN_TO_PYTH_H


struct pythagorean_theorom {
    const float inch = 5;

    static float p_t(float a, float b, float c);

}pyth;

#endif

康寿:

1>------ Build started: Project: ConsoleApplication3, Configuration: Debug Win32 ------
1>ConsoleApplication3.cpp
1>c:\users\lisa\documents\visual studio 2017\projects\consoleapplication3\consoleapplication3\consoleapplication3.cpp(40): error C2297: '+': illegal, right operand has type 'float (__cdecl *)(float,float,float)'
1>c:\users\lisa\documents\visual studio 2017\projects\consoleapplication3\consoleapplication3\consoleapplication3.cpp(60): warning C4244: 'argument': conversion from 'float' to 'int', possible loss of data
1>Done building project "ConsoleApplication3.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

1 个答案:

答案 0 :(得分:0)

编辑: 仍然不确定你想要做什么。 首先,正如其他人的评论所指出的,你需要在某个地方定义这个功能。现在它正在太空中漂浮。使用范围解析运算符::

pythagorean_theorom::p_t(a, b, c);

...

struct pythagorean_theorom {
const float inch = 5;

static float p_t(float a, float b, float c);

};

...

float pythagorean_theorom::p_t(float a, float b, float c) {

...

这里主要是删除图形内容,猜测你想要的东西:

int main() {
float a, b, c;
float x1, x2, x3;
float y1, y2, y3;


cout << "Enter a,b,c of pythagorean theorom and 0 \n for the value you want to find." << endl;
cout << "Enter Value 'a' \n";
cin >> a;
cout << "Enter Value 'b' \n";
cin >> b;
cout << "Enter Value 'c' \n";
cin >> c;

float foundvalue = pythagorean_theorom::p_t(a, b, c);
//consol output
if (a == 0) {
    cout << "a = " << foundvalue;

    float x1 = 200; //a
    float y1 = 100 + a; //a

    float x2 = 200 + b; //b
    float y2 = 100; //b

    float x3 = 200; //c
    float y3 = 100 - c; //c
}
else if (b == 0) {
    cout << "b = " << foundvalue;
}
else if (c == 0) {
    cout << "c = " << foundvalue;
}
else {
    cout << "You failed... i am disapointed";
}

return 0;

}

看起来你正在尝试返回一个函数,这在这里是非法的。

您需要使用()运算符调用该函数。

e.g。 pyth.p_t(a,b,c);

另外,你不想要^ 2 + b ^ 2 = c ^ 2? 如果你的Pythagoream Theorem.cpp是

,你需要改变第三个
float pyth_a_2 = (pow(a,2)) + (pow(b,2));