定義了主函式與兩個個子函式,其中一個子函式又呼叫另一個子函式,編譯錯了,好久解決不了,代碼的功能就不細說了,都是些簡單的不行的東西,很low,求幫助解決這個問題,感激不盡
主函式編譯時:undefined reference to words(std::string)
words.cpp編譯時:undefined reference to judge(std::string)
主函式代碼:
#include<iostream>
#include<string>
#include<stdlib.h>
#include<fstream>
#include"judge.h"
#include"words.h"
#define KEY_LEN 9//關鍵字串列長度
#define Boundary_LEN 24//界符串列長度
using namespace std;
typedef struct Result{
int str_out;
Result *next;
}Result;
Result *result1=(Result *)new(Result);
Result *result2;
Result *Head=result1;
Result *p=Head;
int words(string str);
int judge(string str_in);
string Keywords[] = {"int","main","void","if","else","char","float","double","return"};//關鍵字串列
string Boundary[] = {">=","<=","==","=",">","<","+","-","*","/","{","}",",",";","(",")","[","]","%","^","&","|","?",":"};//界符串列
int main(){
string str;
char sentence[100];
ifstream fin;
fin.open("F:\\cWorkspace\\Compiler\\first\\source.cpp");
while (!fin.eof())
{
fin.getline(sentence,100);
str=sentence;
if(str=="")//跳過空行
continue;
cout<<str<<endl;
//cout<<fin.eof()<<endl;
words(str);
while(p->next!=NULL){
if(p->str_out>=10)
cout<<"<"<<p->str_out<<">"<<",";
else
cout<<"<0"<<p->str_out<<">"<<",";
p=p->next;
}
cout<<endl;
}
fin.close();
}
judge.h頭檔案的代碼如下
#include<iostream>
#include<string>
#include<stdlib.h>
#include<fstream>
using namespace std;
#ifndef JUDGE_H
#define JUDGE_H
int judge(string str_in);
#endif
words.h頭檔案代碼如下
#include<iostream>
#include<string>
#include<stdlib.h>
#include<fstream>
#include"judge.h"
using namespace std;
#ifndef WORDS_H
#define WORDS_H
int words(string str);
#endif
judge.cpp
#include<iostream>
#include<string>
#include<stdlib.h>
#include<fstream>
#define KEY_LEN 9//關鍵字串列長度
#define Boundary_LEN 24//界符串列長度
using namespace std;
typedef struct Result{
int str_out;
Result *next;
}Result;
Result *result1=(Result *)new(Result);
Result *result2;
Result *Head=result1;
string Keywords[] = {"int","main","void","if","else","char","float","double","return"};//關鍵字串列
string Boundary[] = {">=","<=","==","=",">","<","+","-","*","/","{","}",",",";","(",")","[","]","%","^","&","|","?",":"};//界符串列
//string s_Double[] = (">","<","=");
int judge(string str_in){
//cout<<str_in<<endl;
result2 = (Result *)new(Result);//為下一個結果申請存盤空間
result2->next=NULL;
int i;
for(i=0;i<KEY_LEN;i++)//判斷是否為關鍵字
{
if(str_in==Keywords[i]){
result1->str_out=i+4;
//cout<<result1->str_out<<endl;
result1->next=result2;
result1=result2;
result2=NULL;
return 0;
}
}
for(i=0;i<Boundary_LEN;i++)//判斷是否為界符
{
if(str_in==Boundary[i]){
result1->str_out=i+12;
//cout<<result1->str_out<<endl;
result1->next=result2;
result1=result2;
result2=NULL;
return 0;
}
}
if(str_in[0]=='\''&&str_in[str_in.length()-1]=='\''){//判斷是否為字符
result1->str_out=1;
//cout<<result1->str_out<<endl;
result1->next=result2;
result1=result2;
result2=NULL;
return 0;
}
else if(str_in[0]=='\"'&&str_in[str_in.length()-1]=='\"'){//判斷是否為字串
result1->str_out=2;
//cout<<result1->str_out<<endl;
result1->next=result2;
result1=result2;
result2=NULL;
return 0;
}
else if(isalpha(str_in[0])==1||isalpha(str_in[0])==2){//判斷是否為標示符
result1->str_out=0;
//cout<<result1->str_out<<endl;
result1->next=result2;
result1=result2;
result2=NULL;
return 0;
}
else if( isdigit(str_in[0])){
//數字錯誤處理
int ch;
for(int str_len=0;str_len<str_in.length();str_len++){
ch=str_in[str_len];
//數字的中間出現了除小數點之外的字符說明有錯
if ((str_len != str_in.length() - 1)&&(ch != '.')&&(ch<'0'||ch>'9')) {
cout << "錯誤,不合法的數字輸入,編譯錯誤,請改正錯誤后重新編譯!" << endl;
exit(0);
}
//數字的最后一個為數字或型別標志之外的其他字符說明有錯
else if((str_len == str_in.length() - 1) && ch != 'l'&& ch != 'd' && ch != 'f'&&(ch<'0' || ch>'9')){
cout << "錯誤,不合法的數字輸入,編譯錯誤,請改正錯誤后重新編譯!" << endl;
exit(0);
}
//數字的最后一個可以是l,d,f用來表示其型別
/*else {
continue;
}*/
}
result1->str_out = 3;
//cout<<result1->str_out<<endl;
result1->next=result2;
result1=result2;
result2=NULL;
}
result1->next=NULL;
}
words.cpp
#include<iostream>
#include<string>
#include<stdlib.h>
#include<fstream>
#include"judge.h"
#define KEY_LEN 9//關鍵字串列長度
#define Boundary_LEN 24//界符串列長度
using namespace std;
typedef struct Result{
int str_out;
Result *next;
}Result;
Result *result1=(Result *)new(Result);
Result *result2;
Result *Head;
string Keywords[] = {"int","main","void","if","else","char","float","double","return"};//關鍵字串列
string Boundary[] = {">=","<=","==","=",">","<","+","-","*","/","{","}",",",";","(",")","[","]","%","^","&","|","?",":"};//界符串列
//string s_Double[] = (">","<","=");
int words(string str){
bool isBoundary(char bound);
bool EMP=true;
string str1;
//cout<<EMP<<endl;
int i;
for(i=0;i<str.length();i++){//判斷某行是否為空(只有空格)
if(str[i]!=' '){
EMP=false;
//cout<<EMP<<endl;
break;
}
}
//cout<<i<<endl<<EMP<<endl;
//cout<<"非空"<<endl;
if(isalpha(str[i])!=2&&str[i]!='/'&&str[i]!='{'&&str[i]!='}'&&str[i]!=';'&&str[i]!=' '){
cout<<"編譯錯誤,請檢查程式是否有非法輸入后重新編譯!"<<endl;
exit(0);
}
if(!EMP){//如果該行非空且開頭沒有語法錯誤,對該行進行編譯
//cout<<"開始編譯"<<endl;
int j;
for(i;i<str.length();i++){//行開頭空格已跳過
if(str[i]==' '){//跳過單詞間的空格
continue;
}
//cout<<"界符判斷"<<endl;
bool isboundary=isBoundary(str[i]);//判斷當前字符是否為界符
if(isboundary){
string str2;
str2+=str[i];
if(str[i+1]=='='){
i++;
str2+=str[i];
}
//cout<<str2<<endl;
judge(str2);
str2="";
continue;
}
j=i;
while(!isboundary&&str[j]!=' '){//當前字符不是界符且不是空格,加入待判斷串
str1=str1+str[j];
//cout<<str1;
j++;
bool isboundary=isBoundary(str[j]);
if(isboundary)
break;
}
if(str1.length()!=0){//當前待判斷的串不為空
judge(str1);
}
str1="";//當前字串判斷過之后置空
i=--j;
}
}
//cout<<str.length();
//while(str)
//fin.close();
//cout<<sentence;
}
bool isBoundary(char bound){
//cout<<"已進入界符判斷"<<bound<<endl;
string Bound;
Bound+=bound;
for(int j=0;j<Boundary_LEN;j++){
if(Bound==Boundary[j])
return true;
}
return false;
}
uj5u.com熱心網友回復:
編譯的時候兩個檔案都寫上cl words.cpp 主函式的cpp
g++ words.cpp 主函式的cpp
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/90238.html
標籤:基礎類
