2016-03-25 17 views
1

私は、Studentクラスと、cppファイルの関数実装でヘッダーファイルに定義されたNameクラスを持っています。 Visual Studioの2015でコンパイルするとき、私は、次のエラーを取得:C++ oop複数のリンカーエラー

ここ
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 &)" ([email protected][email protected][email protected]@[email protected]@@[email protected]@[email protected]@@@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 &)" ([email protected][email protected][email protected]@[email protected]@@[email protected]@[email protected]@@@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 

Student.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 

Name.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; 
} 

Student.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; 
} 

あなたが過負荷にoperator<<operator>>を宣言した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; 
    } 
} 

答えて

0

Studentクラスですが、.cpで定義したことはありませんpファイル。私はこれがあなたが見ているエラーだと思います。

私はあなたがそれを編集した前に、それがどのように見えるかわからないんだけど、あなたはName.hファイル(だけでなく、あなたのMain.cppファイル)に<iostream>を含むことをする必要があります

1

#include.hあなたmain()ファイル内のファイルおよびNOT .cppファイル

+0

私はこれを変更した、と私は」依然としてリンカエラーが発生する – mikotochan

+0

ソリューションをクリーン/ビルドします。 Build-> Rebuildに行き、もう一度やり直してください。 main()のすべてのヘッダファイルもインクルードしてください(私はあなたがすべきだと思います) –

+0

私はこれを無駄にしました:/ – mikotochan

関連する問題