【问题标题】:undefined reference to `readVector(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'未定义的对 `readVector(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)' 的引用
【发布时间】:2017-05-08 17:53:29
【问题描述】:

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

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 toreadVector(std::__cxx11::basic_string, std::allocator >)' clang: 错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)makefile: 5:目标“matvec”的配方失败 make: *** [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;
}

【问题讨论】:

  • 在您未显示的代码中的某处,您有一个 readVector 声明,带有一个 std::string 参数。这是您实际使用 readVector(inputfile) 调用的重载,但您尚未实现它。
  • 很明显你的输入类型有误:std::string inputfile 不是std::ifstreamstd::istream

标签: c++ c++11


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2021-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多