【问题标题】:C++ oop multiple linker errorsC++ oop 多个链接器错误
【发布时间】:2016-03-25 19:32:30
【问题描述】:

我在头文件中定义了一个 Student 类和一个 Name 类,并在 cpp 文件中实现了函数。使用 Visual Studio 2015 编译时出现以下错误:

Severity    Code    Description Project File    Line
Error   LNK2019 unresolved external symbol "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class Student const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABVStudent@@@Z) referenced in function _main    3512-lab8   c:\Users\Tess\documents\visual studio 2015\Projects\3512-lab8\3512-lab8\main.obj    1
Error   LNK2019 unresolved external symbol "class std::basic_istream<char,struct std::char_traits<char> > & __cdecl operator>>(class std::basic_istream<char,struct std::char_traits<char> > &,class Student &)" (??5@YAAAV?$basic_istream@DU?$char_traits@D@std@@@std@@AAV01@AAVStudent@@@Z) referenced in function _main  3512-lab8   c:\Users\Tess\documents\visual studio 2015\Projects\3512-lab8\3512-lab8\main.obj    1
Error   LNK1120 2 unresolved externals  3512-lab8   c:\users\tess\documents\visual studio 2015\Projects\3512-lab8\Debug\3512-lab8.exe   1

这是我的文件

名称.h

#include <string>
#ifndef NAME_H
#define NAME_H
class Name {
public:
    Name() {}
    Name(const std::string& first, const std::string& last)   :first_(toLowerCase(first)), last_(toLowerCase(last)) {}
    std::string toLowerCase(const std::string& s);
    friend std::istream& operator>>(std::istream& is, Name& n);
    friend std::ostream& operator<<(std::ostream& os, const Name& n);
    friend bool isValidName(const Name& n);
private:
    std::string first_;
    std::string last_;
    static bool isValidName(const Name& n);
};
#endif

学生.h

#include "Name.h"
#include <string>
#ifndef STUDENT_H
#define STUDENT_H
class Student {
public:
    Student(){}
    explicit Student(const Name& name, const std::string& id = "A11111111") :id_(id), name_(name) {
    if (!isValidId(id_)) {
        throw "invalid id";
    }
    else if (!isValidName(name_)) {
        throw "invalid name";
    }
}
    virtual ~Student(){}
    friend std::ostream& operator<<(std::ostream& os, const Student& s);
    friend std::istream& operator>>(std::istream& is, Student& s);
    friend bool operator<(const Student& lhs, const Student& rhs);
private:
    std::string id_;
    Name name_;
    static bool isValidId(const std::string& id);
};
#endif

名称.cpp

#include "Name.h"
std::istream& operator>>(std::istream& is, Name& n) {
    return is >> n.first_ >> n.last_;
}
std::ostream& operator<<(std::ostream& os, const Name& n) {
    return os << n.first_ << " " << n.last_;
}
std::string Name::toLowerCase(const std::string& s) {
    std::string str = s;
    for (std::string::size_type i = 0; i < s.size(); i++) {
        tolower(str[i]);
    }
    return str;
}
bool Name::isValidName(const Name & n){
    std::string::size_type i;
    std::string::size_type first_size = n.first_.size();
    std::string::size_type last_size = n.last_.size();

    if (first_size == 0 || last_size == 0) {
        return false;
    }
    for (i = 0; i < first_size; ++i) {
        if (!isalpha(n.first_[i])) {
            return false;
        }
    }
    for (i = 0; i < last_size; ++i) {
        if (!isalpha(n.last_[i])) {
            return false;
        }
    }
    return true;
}

学生.cpp

#include "Student.h"
#define SID_LENGTH 9
bool operator<(const Student& lhs, const Student& rhs) {
    return lhs.id_ < rhs.id_;
}
bool Student::isValidId(const std::string & id){
    std::string::size_type i = 0;

    if (id.length() == SID_LENGTH) {
        if (id[i] == 'A' || id[i] == 'a') {
            for (i = 1; i < id.size(); i++) {
                if (!isdigit(id[i])) {
                    return false;
                } 
            }
            return true;
        }
    }
    return false;
}

Main.cpp

#include "Student.h"
#include <iostream>
#include <map>
int main() {
    std::map<Student, int> m;
    Student s;
    int score;
    while (std::cin >> s >> score) {
        m[s] += score;
    }
    for (const auto& it : m) {
        std::cout << it.first << "" << it.second << std::endl;
    }
}

【问题讨论】:

    标签: c++ visual-studio oop linker-errors


    【解决方案1】:

    #include .h 文件在您的 main() 文件中,而不是 .cpp 文件中

    【讨论】:

    • 我已更改此设置,但仍然出现一些链接器错误
    • 清理/构建您的解决方案。转到 Build->Rebuild 并重试?还要在 main() 中包含所有头文件(我认为你应该)
    • 在 main 中包含所有头文件?
    【解决方案2】:

    您已经为 Student 类声明了重载的 operator&lt;&lt; 和 operator&gt;&gt;,但您从未在 .cpp 文件中定义它们。我认为这是您看到的错误。

    我不确定它在您编辑之前是什么样子,但您确实需要在您的 Name.h 文件(以及您的 Main.cpp 文件)中包含 &lt;iostream&gt;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-07
      • 2011-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-28
      相关资源
      最近更新 更多