0

EDIT:thanks for pointing me in the right direction of circular dependency. I tried removing #include "datastructure.h" from Mathfg.h, and I added a "Class ArithmeticTree" in "Matfg.h" to forward declare it, but I get another error inside the CleanExpression function:
cannot convert 'std::__cxx11::basic_string::iterator {aka __gnu_cxx::__normal_iterator >}' to 'const char*' for argument '1' to 'int remove(const char*)'. When I hover over the word "remove", Qt tells me I have "too many arguments". This is on the one line of code that Calculator::CleanExpression comprises.

I am writing a math parser and I have created a header file Mathfg which includes a Calculator class that handles inputs. So far I have a static function called CleanExpression which simply removes whitespace from the input string.

I saw a somewhat similar problem here: Static function declared but not defined in C++, but I could not apply the answers there to my own problem.

(I left out irrelevant sections such as the implementation of the Node struct, but it all works, except for the CleanExpression call.)

Mathfg.h

#ifndef MATHFG_H
#define MATHFG_H

#include <string>
#include "datastructure.h"
using namespace std;

class Mathfg
{
private:
    Mathfg();
};

class Calculator
{
private:
    Calculator();
public:
    static void CleanExpression(string& expression)
    {
        expression.erase(remove(expression.begin(), expression.end(), ' '), expression.end());
    }
};

#endif // MATHFG_H

Note: I do not use a source file for Mathfg.h.

Datastructure.h

In another header, "Datastructure.h", I fix the input and produce an output. I wanted to call the static function CleanExpression from "Mathfg.h", but I get the error: 'Calculator' has not been declared.

#ifndef DATASTRUCTURE_H
#define DATASTRUCTURE_H

#include <iostream>
#include <string>
#include "mfg\mathfg.h"
using namespace std;

struct ArithmeticTree
{
private:
    struct Node
    {
        // Doing some unrelated stuff here
    };

public:
    ArithmeticTree()
    {

    }
    ArithmeticTree(string expression_in)
    {
        Calculator::CleanExpression(expression_in);     // <- Here's the problem!
        // Doing some unrelated stuff here
    }

    // Doing some unrelated stuff here
};

#endif // DATASTRUCTURE_H

Note: I do not use a source file for Datastructure.h.

What does work:

In my game loop, I can successfully run this code:

std::string str = "2 +3-4 /      5";
cout << "Cleaning string:\t'" << str << "'\n";
Calculator::CleanExpression(str);
cout << "Cleaned:\t\t'" << str << "'\n";

...which outputs:

Cleaning string:    '2 +3-4 /      5'
Cleaned:            '2+3-4/5'

...but when I try to call Calculator::CleanExpression from within "Datastructure.h" - or more specifically, from within ArithmeticTree's constructor - I get the aforementioned error. I never created a Calculator or Mathfg object anywhere in my code; I just call its static function like I would normally expect it to behave.

I am sure I have misunderstood how static functions work in C++, which probably also is the cause of my error.

I would really like some tips on how to go about fixing this problem!

I am using Qt Creator 4.2.1 and MinGW 5.3.0 32bit2.

Community
  • 1
  • 1
  • 3
    You have a circular header file dependency... `mathfg.h` depends on `datastructure.h` which depends on `mathfg.h` and so on and on. You need to break the dependency chain somehow, like not including `datastructure.h` in `mathfg.h` (since `mathfg.h` doesn't seem to actually need anything from `datastructure.h`). – Some programmer dude Apr 17 '17 at 11:33
  • Thanks for pointing that out! I edited the question to reflect my changes in the code, but this still doesn't seem to fix the problem. – Dexter André Apr 17 '17 at 12:34

0 Answers0