Примечание: аналогичные вопросы были заданы, но почти все, что я обнаружил, похоже, напрямую используют указатель, который здесь может не иметь отношения
Для этого фрагмента кода
#include <iostream>
#include <string>
#include <vector>
#include <map>
using namespace std;
class Unit{
public:
string name;
char value;
vector<Unit> inner;
Unit(string Name){
name=Name;
}
Unit(char Value){
name="Character";
}
};
class Instruction{
public:
int size;
string type;
string value;
map<string, vector<Unit>> vars;
vector<Instruction> inner;
vector<Instruction> outer;
Instruction(string Type,string Value,vector<Instruction>& Outer,map<string, vector<Unit>> &Vars){
type=Type;
value=Value;
outer=Outer;
vars=Vars;
size=0;
}
Instruction(string Type,vector<Instruction>& Outer,map<string, vector<Unit>> &Vars){
type=Type;
outer=Outer;
vars=Vars;
size=0;
}
bool matches(vector<Unit> &info,int position=0, int vectLocation=0){
cout<<value<<'\n'; //Segmentation Fault Occurs Here
return false;
}
};
void breakUp(vector<Unit>&u,string s){
for(int i=0;i<s.size();i++){
u.push_back(Unit(s[i]));
}
}
int main(){
//String to be matched
vector<Unit> v;
breakUp(v,"For the love of tests");
//Instruction Vector
vector<Instruction> i;
//Var Vector
map<string, vector<Unit>> x;
//Add initial instruction
Instruction constant("Constant","",i,x);
constant.inner.push_back(Instruction("String","For the love of tests",constant.inner,x));
//Test match
bool result=i[0].matches(v);
if(result==true){
cout<<"Match True\n";
}else{
cout<<"Match False\n";
}
return 0;
}
Я получаю ошибку сегментации при попытке доступа к одному из элементов данных в функции matches
.Это также происходит, когда он пытается напечатать size
или type
.Я не смог определить точную причину этой проблемы с памятью и оценил бы любой совет или соображения.