我有以下課程:
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
編輯:我在下面的答案中解決了這個問題。
uj5u.com熱心網友回復:
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) );
}
uj5u.com熱心網友回復:
解決方案是為 parser.o 添加一條規則來編譯 parser.cc:
parser.o: parser.cc
g -std=c 11 -g -c $< -o parser.o
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/425965.html
上一篇:異步似乎不使用多執行緒C
