简体   繁体   中英

Header functions not found in C++

I've been trying to make a simple graphics calculator in SFML (although this issue isn't SFML related). So I made a library that can turn a string equation into a double answer. But when I try to add said library (StringDouble.h ans StringDouble.cpp) to main.cpp, I get:

D:\SFML projects\Calculator\main.cpp||In function 'double calculateString(std::string)':|
D:\SFML projects\Calculator\main.cpp|30|error: 'Mult' was not declared in this scope|
D:\SFML projects\Calculator\main.cpp|31|error: 'Div' was not declared in this scope|
D:\SFML projects\Calculator\main.cpp|35|error: 'Addify' was not declared in this scope|
D:\SFML projects\Calculator\main.cpp|36|error: 'Sub' was not declared in this scope|
D:\SFML projects\Calculator\main.cpp|38|error: 'S2D' was not declared in this scope|
D:\SFML projects\Calculator\main.cpp||In function 'int main()':|
D:\SFML projects\Calculator\main.cpp|88|error: 'D2S' was not declared in this scope|
D:\SFML projects\Calculator\main.cpp||In function 'double calculateString(std::string)':|
D:\SFML projects\Calculator\main.cpp|39|warning: control reaches end of non-void function [-Wreturn-type]|
||=== Build finished: 6 errors, 1 warnings (0 minutes, 0 seconds) ===|

This is strange, because I have another linked header file Button.h and Button.cpp. It's most likely a dumb mistake, but I've been staring at the code for hours and found nothing:

main.cpp

#include "SFML/Graphics.hpp"
#include "Button.h"
#include <iostream>
#include <sstream>
#include "StringDouble.h"
using namespace std;

double calculateString(string currStr)
{

    for (unsigned int i=0;i<currStr.size();i++)
    {
        if (currStr[i]=='*') currStr=Mult(currStr, i);
        if (currStr[i]=='/') currStr=Div(currStr, i);
    }
    for (unsigned int i=0;i<currStr.size();i++)
    {
        if (currStr[i]=='+') currStr=Addify(currStr, i);
        if (currStr[i]=='/') currStr=Sub(currStr, i);
    }
    return S2D(currStr);
}

int main()
{
    bool running=1;
    sf::Event event;
    sf::RenderWindow window(sf::VideoMode(212, 284), "Calculator");
    sf::Font font;
//    ((sf::Image&)font->GetImage(30U)).SetSmooth(false);
    sf::Color CharColor(160, 160, 160, 255);
    double curnum=0;
    if (!font.loadFromFile("trebuc.ttf")) cout<<"Your font is shitting itself"<<endl;
    window.setFramerateLimit(30);


    NumButton nums[10];
    Button curnumbut(12, 30, 190, 50);
    curnumbut.setColors(sf::Color(230, 239, 251), sf::Color::Black, sf::Color::Black);
    curnumbut.setText("0", font, 16, sf::Color::Black);


    pmtdButton pmtd[7];
    pmtd[0].initialize('+', font, sf::FloatRect(126, 246, 32, 26));
    pmtd[1].initialize('-', font, sf::FloatRect(126, 214, 32, 26));
    pmtd[2].initialize('*', font, sf::FloatRect(126, 182, 32, 26));
    pmtd[3].initialize('/', font, sf::FloatRect(126, 150, 32, 26));
    pmtd[4].initialize('%', font, sf::FloatRect(162, 150, 32, 26));
    pmtd[5].initialize('.', font, sf::FloatRect(88, 246, 32, 26));


    for (int i=0;i<10;i++)
        nums[i].initialize(i, font);

    while (running)
    {
        while (window.pollEvent(event))
            if (event.type==sf::Event::Closed) running=0;
//        test.checkMouse(sf::Mouse::getPosition(window));
//        test.updateColor();
        for (int i=0;i<10;i++)
        {
            if (nums[i].checkMouse(sf::Mouse::getPosition(window))) addnum(curnum, nums[i]);
            nums[i].updateColor();
        }
        for (int i=0;i<6;i++)
        {
            if (pmtd[i].checkMouse(sf::Mouse::getPosition(window))) {}
            pmtd[i].updateColor();
        }
        curnumbut.setText(D2S(curnum), font, 16, sf::Color::Black);
        window.clear();
        for (int i=0;i<10;i++)
            window.draw(nums[i]);
        for (int i=0;i<6;i++)
            window.draw(pmtd[i]);
        window.draw(curnumbut);
        window.display();
//        cout<<curnum<<endl;
    }
    window.close();
    return 0;
}

StringDouble.h

#ifndef ADD_H
#define ADD_H


struct doubleReturn
{
    double x, y;
};

double S2D (std::string a);

std::string D2S (double number);

doubleReturn GetPrevDouble(std::string x, int pos)

doubleReturn GetNextDouble(std::string x, int pos)

std::string Addify(std::string x, int pos);
std::string Sub(std::string x, int pos);
std::string Mult(std::string x, int pos);
std::string Div(std::string x, int pos);


#endif

StringDouble.cpp

#include <sstream>
#include <iostream>
#include "StringDouble.h"
//using namespace std;

double S2D (std::string a)
{
    return atof(Temperature.c_str());
}

std::string D2S (double number)
{
    ostringstream buff;
    buff<<std::fixed<<number;
    std::string x=buff.str();
    while(x[x.size()-1]=='0')
        x.erase(x.size()-1);
    if (x[x.size()-1]=='.') x.erase(x.size()-1);
    return x;
}

doubleReturn GetPrevDouble(std::string x, int pos)
{
    int i;
    bool flag=0;
    for(i=pos;i>=0;i--)
        if (x[i]<'0'||x[i]>'9') {if (x[i]=='.'&&flag==0) flag=1;
                                 else break;}
    doubleReturn ret.x=S2D(x.substr(i+1, pos-1));
    ret.y=i+1;
}

doubleReturn GetNextDouble(std::string x, int pos)
{
    int i;
    bool flag=0;
    for(i=pos;i<x.size();i++)
        if (x[i]<'0'||x[i]>'9') {if (x[i]=='.'&&flag==0) flag=1;
                                 else break;}
    DoubleReturn ret;
    ret.x=S2D(x.substr(pos+1, i-1));
    ret.y=i-1;
    return ret;
}


std::string Addify (std::string x, int pos)
{
    doubleReturn ret1=GetPrevDouble(x, pos);
    doubleReturn ret2=GetNextDouble(x, pos));

    return x.substr(0, ret1.y)+D2S(ret1+ret2)+x.substr(ret2.y, x.end());
}

std::string Sub (std::string x, int pos)
{
    doubleReturn ret1=GetPrevDouble(x, pos);
    doubleReturn ret2=GetNextDouble(x, pos));

    return x.substr(0, ret1.y)+D2S(ret1-ret2)+x.substr(ret2.y, x.end());
}

std::string Mult (std::string x, int pos)
{
    doubleReturn ret1=GetPrevDouble(x, pos);
    doubleReturn ret2=GetNextDouble(x, pos));

    return x.substr(0, ret1.y)+D2S(ret1*ret2)+x.substr(ret2.y, x.end());
}

std::string Div (std::string x, int pos)
{
    doubleReturn ret1=GetPrevDouble(x, pos);
    doubleReturn ret2=GetNextDouble(x, pos));

    return x.substr(0, ret1.y)+D2S(ret1/ret2)+x.substr(ret2.y, x.end());
}

Any help in pinpointing my most-likely dumb mistake will be welcome. :)

The "dumb mistake" as you call it is missing semicolons after:

doubleReturn GetPrevDouble(std::string x, int pos)

doubleReturn GetNextDouble(std::string x, int pos)

You also need to #include <string>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM