对`readVector(std :: __ cxx11 :: basic_string ,std :: allocator >)的未定义引用

时间:2022-01-04 09:43:20

I got this error message when I tried to compile my code:

我尝试编译代码时收到此错误消息:

c++ -Ofast -march=native -DNDEBUG -std=c++11 -Wc++11-extensions -Wall matvec.o amath483.o Vector.o -o matvec

c ++ -Ofast -march = native -DNDEBUG -std = c ++ 11 -Wc ++ 11-extensions -Wall matvec.o amath483.o Vector.o -o matvec

matvec.o: In function main': matvec.cpp:(.text+0x209): undefined reference toreadVector(std::__cxx11::basic_string, std::allocator >)' clang: error: linker command failed with exit code 1 (use -v to see invocation) makefile:5: recipe for target 'matvec' failed make: *** [matvec] Error 1

matvec.o:在函数main':matvec.cpp :(。text + 0x209):未定义引用toreadVector(std :: __ cxx11 :: basic_string,std :: allocator>)'clang:错误:链接器命令失败,退出代码为1 (使用-v查看调用)makefile:5:目标'matvec'的配方失败make:*** [matvec]错误1

My code is as follows:

我的代码如下:

#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;
}

1 个解决方案

#1


0  

Clearly you have wrong input type: std::string inputfile is not std::ifstream or std::istream. You need to open an input std::ifstream file, whose name stored in inputfile and then pass it to readVector.

显然你有错误的输入类型:std :: string inputfile不是std :: ifstream或std :: istream。您需要打开一个输入std :: ifstream文件,其名称存储在inputfile中,然后将其传递给readVector。

#1


0  

Clearly you have wrong input type: std::string inputfile is not std::ifstream or std::istream. You need to open an input std::ifstream file, whose name stored in inputfile and then pass it to readVector.

显然你有错误的输入类型:std :: string inputfile不是std :: ifstream或std :: istream。您需要打开一个输入std :: ifstream文件,其名称存储在inputfile中,然后将其传递给readVector。