[英]Getting unknown Segmentation Fault "(address) in std::__cxx11::basic_string<char, std::char_traits<char>,..., std::allocator<char> > const&) const ()
[英]undefined reference to `readVector(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
嘗試編譯代碼時收到以下錯誤消息:
c ++ -Ofast -march = native -DNDEBUG -std = c ++ 11 -Wc ++ 11-extensions -Wall matvec.o amath483.o Vector.o -o matvec
matvec.o:在函數
main': matvec.cpp:(.text+0x209): undefined reference to
readVector(std :: __ cxx11 :: basic_string,std :: allocator>)的main': matvec.cpp:(.text+0x209): undefined reference to
'clang:錯誤:鏈接器命令失敗,並帶有退出代碼1(使用-v查看調用)makefile:5:目標“ matvec”的配方失敗:*** [matvec]錯誤1
我的代碼如下:
#include <iostream>
#include <string>
#include <cstdlib>
#include <vector>
#include <fstream>
#include "Vector.hpp"
using namespace std;
Vector readVector(istream& input) {
string string_input;
int n;
getline(input,string_input);
if(string_input.compare("AMATH 583 VECTOR") != 0) exit(-1);
input >> n;
Vector v(n);
for(int i = 0; i < n; i++)
input >> v(i);
getline(input,string_input);
getline(input,string_input);
if(string_input.compare("THIS IS THE END") != 0) exit(-1);
return v;
}
Vector readVector(ifstream& file) {
string string_input;
int n;
getline(file,string_input);
if(string_input.compare("AMATH 583 VECTOR") != 0) exit(-1);
file >> n;
Vector v(n);
for(int i = 0; i < n; i++)
file >> v(i);
getline(file,string_input);
getline(file,string_input);
if(string_input.compare("THIS IS THE END") != 0) exit(-1);
return v;
}
#include "Vector.hpp"
#include <iostream>
#include <fstream>
#include "amath483.hpp"
using namespace std;
int main(int argc, char* argv[]) {
if(argc < 2 || argc > 4) {
cout << "You must provide at least one argument or at most three arguments(including two options)" << endl;
return -1;
}
int inputoption = -1;
int outputoption = -1;
if(argc == 3) {
inputoption = 0;
}
if(argc == 4) {
inputoption = 0;
outputoption = 0;
}
// Check the format
Vector v = NULL;
if(inputoption == 0) {
string inputfile = argv[2];
v = readVector(inputfile);
} else {
v = readVector(cin);
}
for(int i=0; i < v.numRows(); i++)
cout << v(i) << endl;
return 0;
}
顯然,您輸入了錯誤的類型: std::string inputfile
輸入文件不是std::ifstream
或std::istream
。 您需要打開一個輸入std::ifstream
文件,其名稱存儲在inputfile
,然后將其傳遞給readVector
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.