我的成员初始值设定项列表中的语法错误
Syntax error on my member initializer lists
我有以下 class:
class MathNode {
protected:
std::list<std::pair<std::string, MathNode*> > myChildren;
public:
MathNode()
: myChildren{} // line 15
{} // line 16
MathNode(std::string l, MathNode* c)
: myChildren{ std::make_pair(l, c) }
{}
MathNode(std::string l1, MathNode* c1,
std::string l2, MathNode* c2)
: myChildren{ std::make_pair(l1, c1),
std::make_pair(l2, c2) }
{}
};
当我编译时出现以下错误:
In file included from ./calc.hpp:6:
./nodes.hpp:16:9: error: expected member name or ';' after declaration
specifiers
{}
^
./nodes.hpp:15:25: error: expected '('
: myChildren{}
我尝试在第 15 行将花括号更改为圆括号,但出现以下错误:
In file included from ./calc.hpp:6:
./nodes.hpp:19:9: error: expected member name or ';' after declaration
specifiers
{}
^
./nodes.hpp:18:25: error: expected '('
: myChildren{ std::make_pair(l, c) }
^
./nodes.hpp:18:47: error: expected ';' after expression
: myChildren{ std::make_pair(l, c) }
据我所知,我使用的是完全合法的 C++11 语法,我什至在我的 Makefile 中明确传递了使用 C++11 的选项。给出了什么?
编辑:我们已将问题缩小为 Makefile 问题。这是生成文件:
all: calc test
calc: parser.o lexer.o calc.o
g++ -std=c++11 -g -o calc calc.o lexer.o parser.o
test: calc
./calc input.txt
%.o: %.cpp
g++ -std=c++11 -g -c $<
lexer.o: lexer.yy.cc
g++ -std=c++11 -g -c $< -o lexer.o
lexer.yy.cc: grammar.hh lexer.l
flex --outfile lexer.yy.cc lexer.l
grammar.hh: parser.yy
bison --defines=grammar.hh -d -v parser.yy
parser.cc: grammar.hh
.PHONY: clean test
clean:
rm -rf *.o *.cc *.hh calc parser.output
编辑:我在下面的答案中解决了这个问题。
std::list
有自己的默认构造函数,所以你的默认构造函数根本不需要显式初始化myChildren
,例如:
MathNode() {}
或者,在 C++11 及更高版本中:
MathNode() = default;
在你的其他构造函数中,你需要 C++11 或更高版本才能使用你想要的初始化语法。正如您推断的那样,您没有为 C++11 进行编译,这就是为什么您在 C++11 语法上遇到错误的原因。您需要修复您的 makefile 以正确启用 C++11。
否则,只需在构造函数主体中使用 push_back()
即可,例如:
MathNode(std::string l, MathNode* c)
{
myChildren.push_back( std::make_pair(l, c) );
}
MathNode(std::string l1, MathNode* c1,
std::string l2, MathNode* c2)
{
myChildren.push_back( std::make_pair(l1, c1) );
myChildren.push_back( std::make_pair(l2, c2) );
}
解决方案是为 parser.o 添加一条规则来编译 parser.cc:
parser.o: parser.cc
g++ -std=c++11 -g -c $< -o parser.o
我有以下 class:
class MathNode {
protected:
std::list<std::pair<std::string, MathNode*> > myChildren;
public:
MathNode()
: myChildren{} // line 15
{} // line 16
MathNode(std::string l, MathNode* c)
: myChildren{ std::make_pair(l, c) }
{}
MathNode(std::string l1, MathNode* c1,
std::string l2, MathNode* c2)
: myChildren{ std::make_pair(l1, c1),
std::make_pair(l2, c2) }
{}
};
当我编译时出现以下错误:
In file included from ./calc.hpp:6:
./nodes.hpp:16:9: error: expected member name or ';' after declaration
specifiers
{}
^
./nodes.hpp:15:25: error: expected '('
: myChildren{}
我尝试在第 15 行将花括号更改为圆括号,但出现以下错误:
In file included from ./calc.hpp:6:
./nodes.hpp:19:9: error: expected member name or ';' after declaration
specifiers
{}
^
./nodes.hpp:18:25: error: expected '('
: myChildren{ std::make_pair(l, c) }
^
./nodes.hpp:18:47: error: expected ';' after expression
: myChildren{ std::make_pair(l, c) }
据我所知,我使用的是完全合法的 C++11 语法,我什至在我的 Makefile 中明确传递了使用 C++11 的选项。给出了什么?
编辑:我们已将问题缩小为 Makefile 问题。这是生成文件:
all: calc test
calc: parser.o lexer.o calc.o
g++ -std=c++11 -g -o calc calc.o lexer.o parser.o
test: calc
./calc input.txt
%.o: %.cpp
g++ -std=c++11 -g -c $<
lexer.o: lexer.yy.cc
g++ -std=c++11 -g -c $< -o lexer.o
lexer.yy.cc: grammar.hh lexer.l
flex --outfile lexer.yy.cc lexer.l
grammar.hh: parser.yy
bison --defines=grammar.hh -d -v parser.yy
parser.cc: grammar.hh
.PHONY: clean test
clean:
rm -rf *.o *.cc *.hh calc parser.output
编辑:我在下面的答案中解决了这个问题。
std::list
有自己的默认构造函数,所以你的默认构造函数根本不需要显式初始化myChildren
,例如:
MathNode() {}
或者,在 C++11 及更高版本中:
MathNode() = default;
在你的其他构造函数中,你需要 C++11 或更高版本才能使用你想要的初始化语法。正如您推断的那样,您没有为 C++11 进行编译,这就是为什么您在 C++11 语法上遇到错误的原因。您需要修复您的 makefile 以正确启用 C++11。
否则,只需在构造函数主体中使用 push_back()
即可,例如:
MathNode(std::string l, MathNode* c)
{
myChildren.push_back( std::make_pair(l, c) );
}
MathNode(std::string l1, MathNode* c1,
std::string l2, MathNode* c2)
{
myChildren.push_back( std::make_pair(l1, c1) );
myChildren.push_back( std::make_pair(l2, c2) );
}
解决方案是为 parser.o 添加一条规则来编译 parser.cc:
parser.o: parser.cc
g++ -std=c++11 -g -c $< -o parser.o