gpt4 book ai didi

C++ oop 多个链接器错误

转载 作者:行者123 更新时间:2023-11-30 05:29:57 25 4
gpt4 key购买 nike

我在头文件中定义了一个 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;
}
}

最佳答案

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

关于C++ oop 多个链接器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36226694/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com