我對 C 很陌生,所以我有一個作業,并在處理構建 AST 樹的程序之前嘗試進行一些實驗。嘗試輸入一個字串表達(運算子和運算元)并將其拆分為在 python 拆分中,但似乎我一直遇到基本類結構問題或不確定的編譯問題。
expr.h 檔案
#include <string>
class BuildAst
{
public:
void build(std::string expr);
protected:
char opera;
char left_child;
char right_child;
};
expr.cpp 檔案
#include "expr.h"
#include <string>
#include <string.h>
#include <iostream>
#include <sstream>
void BuildAst::build(std::string expr)
{
std::string str1, str2;
std::stringstream s(expr);
s>>str1>>str1;
std::cout<<str1<<std::endl;
std::cout<<str2<<std::endl;
};
main.cpp 檔案
#include "expr.h"
#include <vector>
#include <string>
#include <iostream>
int main() {
std::string s;
std::cout << "Enter the expression\n";
std::cin >> s;
BuildAst ast;
ast.build(s);
};
生成檔案
CFLAGS="--std=c 14"
all: main tests
expr:
$(CC) $(CFLAGS) -c -o expr.o expr.cpp
main: expr
$(CC) $(CFLAGS) -o expr main.cpp expr.o
tests: expr
$(CC) $(CFLAGS) -o tests testcases.cpp expr.o
clean:
rm *.o expr tests
我嘗試使用的輸入運算式是 3 5,輸出應該是 3,5 而且我不斷收到以下錯誤

嘗試添加到 3 個檔案 (main.cpp/expr.cpp/expr.h)
#define _GLIBCXX_USE_CXX11_ABI 0/1
然后我改成的錯誤

你能幫我嗎,謝謝。
uj5u.com熱心網友回復:
不久前我有一個類似的問題。robthebloke為我提供了完美的答案。不確定這是否適用于你,但我的帖子鏈接在這里。
uj5u.com熱心網友回復:
好的,結果我沒有將檔案作為代碼塊中的專案運行。問題已解決
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/373607.html
