0

I encountered a weird error when building my code:

Before I show you what the error was, I'd like to show you what the code is doing. This is a node class for my little cmd project, each node is a file/folder. I wrote a function to show the info of the node.

// Node.h
#include <string>
#include <memory>

using namespace std;

typedef bool   NODE_TYPE;
#define FILE   true
#define DIR    false

class Node
{
private:
    string _node_name;
    NODE_TYPE _node_type;
    string _create_date;
    string _create_user;
    weak_ptr<Node> _parent;
    shared_ptr<Node> _sibling;
    shared_ptr<Node> _child;
public:
    /*Constructors, Destructors and other functions*/
    const string toString();
};

// Node.cpp
#include "Node.h"
#include <string>
#include <sstream>
using namespace std;

const string Node::toString()
{
    ostringstream buffer;
    buffer << ...  // the data member of Node
    return buffer.str();
}

I wrote a test in main.cpp, and when I say g++ Node.cpp main.cpp -Wall -std=c++11, here's the error:

In file included from Node.cpp:6:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/sstream:184:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/istream:163:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/ostream:138:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/ios:214:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/__locale:40:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/xlocale.h:93:
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/xlocale/_stdio.h:32:33: error: expected expression
int      fprintf_l(FILE * __restrict, locale_t __restrict, const char * __restrict, ...)

Errors like this are 21 more, I guess there was something wrong with SDK, so I reinstalled it, however still no work. Could anyone help me?

LiuYuan
  • 127
  • 9
  • 2
    Not sure but `#define FILE true` is suspicious because `FILE` is a name defined by the C standard library, and it's likely that definition is getting included in your code and conflicting. Don't use macros, they only confuse. If you really want a constant called `FILE` then the way to do that is `constexpr bool FILE = true;` – john Aug 30 '22 at 07:28
  • yes probably that. stdio.h will have a `struct FILE {...}` and now you made it `struct true {...}` and it doesn't compile. Something like that. – user253751 Aug 30 '22 at 07:30
  • In fact the fact that the final error is in a file called `_stdio.h` is the give away. Definitely it's `FILE` that is causing a problem. – john Aug 30 '22 at 07:33
  • @LiuYuan Given that then the `constexpr` option I gave will kind of work, but then you say `using namespace std;` which is perhaps going to lead to another conflict, Try to avoid `using namespace std;`, **especially in header files**. – john Aug 30 '22 at 07:35
  • @john I've written it this way for the sake of brevity, I write `using std::...` in fact. But still, thank you for your suggestion, for I don't know where these two ways differ. – LiuYuan Aug 30 '22 at 07:45
  • @LiuYuan https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice – john Aug 30 '22 at 08:10

1 Answers1

1

I'd like to thank all comments below my question, they are really helpful.

In C standard library, FILE is defined as a structure to which stores the info of a file opened by the program. So my own macro definition leads to wrong macro expansion.

Here I'd like to reference the suggestion by Scott Meyers: For simple constants, prefer const objects or enums to #defines.

LiuYuan
  • 127
  • 9