2

<< 演算子のオーバーロードに関する情報を調べたところ、すべて正しく実行したように見えますが、コンパイル エラーが発生し続けます。私はこの関数をヘッダー ファイルに追加し、cpp ファイルの先頭にプロトタイプを配置しました。

私の大学.h:

#ifndef UNIVERSITY_H
#define UNIVERSITY_H

#include <string>
#include <vector>
#include <iostream>

using namespace std;

#include "Department.h"
#include "Student.h"
#include "Course.h"
#include "Faculty.h"
#include "Person.h"


class University
{
  friend ostream& operator<< (ostream& os, const vector<Department>& D);
  friend ostream& operator<< (ostream& os, const Department& department);

 protected:
  vector<Department> Departments;
  vector<Student> Students;
  vector<Course> Courses;
  vector<Faculty> Faculties;
  static bool failure;
  static bool success;


 public:

  bool CreateNewDepartment(string dName, string dLocation, long dChairID);
  bool ValidFaculty(long dChairID);
};

#endif

私の大学.cpp:

#ifndef UNIVERSITY_CPP
#define UNIVERSITY_CPP

#include<string>
#include<vector>
#include<iostream>
using namespace std;

#include "University.h"

ostream& operator<<(ostream& os, const vector<Department>& D);
ostream& operator<<(ostream& os, const Department& department);

bool University::failure = false;
bool University::success = true;


bool University::CreateNewDepartment(string dName, string dLocation, long dChairID)
{
  if((dChairID != 0) && (ValidFaculty(dChairID)== University::failure))
    {
      Department D(dName, dLocation, dChairID);
      Departments.push_back(D);
      for (int i = 0; i < Departments.size(); i++)
         cout << Departments;
      return University::success;
    }
  return University::failure;
}

bool University::ValidFaculty(long dChairID)
{
  for (int i = 0; i < Faculties.size(); i++)
    {
      if (Faculties[i].ID == dChairID)
        return University::success;
    }
  return University::failure;
}

ostream& operator<<(ostream& os, const vector<Department>& D)
{
  for (int i = 0; i < D.size(); i++)
     os << D[i] << endl;
  os << "\n";
  return os;
}

ostream& operator<< (ostream& os, const Department& department)
{
    department.Print(os);
    return os;
}

#endif

私の部門.h:

#ifndef DEPARTMENT_H
#define DEPARTMENT_H

#include<vector>
#include<string>
#include<iostream>
using namespace std;

class Department
{
  friend class University;
  friend ostream& operator<< (ostream& os, Department& department);

 protected:
  long ID;
  string name;
  string location;
  long chairID;
  static long nextDepartID;

 public:
  Department();
  Department(string dName, string dLocation, long dChairID);
  void Get();
  void Print(ostream& os)const;
};

#endif

私の部門.cpp:

#ifndef DEPARTMENT_CPP
#define DEPARTMENT_CPP

#include<iostream>
#include<string>
using namespace std;

#include "Department.h"

long Department::nextDepartID = 100;

Department::Department()
{
  ID = nextDepartID++;
  name = "Null";
  location = "Null";
  chairID = 0;
}

Department::Department(string dName, string dLocation, long dChairID):name(dName), location(dLocation), chairID(dChairID)
{
  ID = nextDepartID++;
}

void Department::Get()
{
}

void Department::Print(ostream& os)const
{
  os << "\n";
  os << ID << endl;
  os << name << endl;
  os << location << endl;
  os << chairID << endl;
  os <<"\n\n";
}

ostream& operator<< (ostream& os, const Department& department)
{
  department.Print(os);
  return os;
}
#endif

これで、この問題のみに関連するすべてが表示されます。私が今受け取った唯一のエラーは、void 値が無視されていないということです。

エラーのスニペット:

University.cpp: In function ‘std::ostream& operator<<(std::ostream&, const Department&)’:
University.cpp:53: error: void value not ignored as it ought to be
Department.cpp: In function ‘std::ostream& operator<<(std::ostream&, const Department&)’:
Department.cpp:42: error: void value not ignored as it ought to be

最終編集:

私を助けてくれたみんなに感謝します。私は間違いなく、演算子のオーバーロードについてよりよく理解しています...特に、ユーザー定義型の印刷ベクトルを扱う場合は!

4

1 に答える 1

4

ベクトルの内容を反復して出力する関数は正しいかもしれませんが、ベクトルに含まれる実際のオブジェクトにはoperator<<指定がありませんでした。

あなたはそれを持っている必要があります。

クラスで呼び出さPrint()れたメソッドが既にある場合は、次のように単純にオーバーロードを作成できます。Departmentoperator<<

std::ostream& operator<<(std::ostream& os, const Department& department) {
    os<<department.Print();
    return os;
}

更新を投稿する前に、次のコードを準備しました。多分それはあなたを助けることができます。

#include<iostream>
#include<vector>
#include<string>

class Department {
 public:
  Department(const std::string& name)
      : m_name(name) { }
  std::string name() const {
    return m_name;
  }
 private:
  std::string m_name;  
};


// If you were to comment this function, you would receive the
// complaint that there is no operator<< defined.
std::ostream& operator<<(std::ostream& os, const Department& department) {
  os<<"Department(\""<<department.name()<<"\")";
  return os;
}

// This is a simple implementation of a method that will print the
// contents of a vector of arbitrary type (not only vectors, actually:
// any container that supports the range-based iteration): it requires
// C++11.
template<typename T>
void show(const T& container) {
  for(const auto& item : container) {
    std::cout<<item<<std::endl;
  }
}

int main() {
  std::vector<Department> deps = {{"Health"}, {"Defense"}, {"Education"}};
  show(deps);
}

コンパイルしてg++ example.cpp -std=c++11 -Wall -Wextra(OS X 10.7.4とGCC 4.8.1を使用しました)、次を取得します。

$ ./a.out 
Department("Health")
Department("Defense")
Department("Education")
于 2013-11-08T03:40:34.823 に答える