简体   繁体   中英

Why g++ reported an error in C++ Standard Library in MacOS GDK?

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?

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 #define s .

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