Xin chào tất cả các bạn!
Có nhất thiết phải include thêm file.cpp đối với khuôn mẫu lớp trong hàm main() ?
Cụ thể là mình cài đặt một file.h chứa phần khai báo khuôn mẫu lớp.Và một file.cpp chứa phần định nghĩa các hàm thành viên.
=>if{ định nghĩa các hàm thành viên trực tiếp trong phần khai báo lớp thì ngoài hàm main() chỉ phải include file.h thôi }.else{ thì phải include cả 2 file }.
Có nhất thiết phải include thêm file.cpp đối với khuôn mẫu lớp trong hàm main()?
Ta không bao giờ nên include file .cpp trong C++.
- File
.cpplà để chứa định nghĩa, definition, - File
.hlà để chứa khai báo, declaration.
Vâng! Nhưng đối với khuôn mẫu lớp có một điều lạ là: khi e làm như vậy thì bị lỗi~
Em làm lỗi là vì em sai một cái gì đấy. Em phải làm theo những khái niệm căn bản nhất trước, tức là không include file .cpp.
Uh, template thì bạn cứ quăng hết vào file .h, không thì compiler sẽ báo lỗi. Nó có cách để đi vòng như theo bạn nói bằng cách include file cpp. Nguyên nhân và giải thích thì ở trên mạng có nhiều lắm, ví dụ http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file
//Set.h
#pragma once
#include <iostream>
using namespace std;
template <class T, int n>
class Set
{
public:
Set() :count(0){}
Set(Set<T, n> &s){
for (int i = 0; i < s.count; i++)
data[i] = s.data[i];
count = s.count;
}
~Set(){}
Set<T, n> & operator=(Set<T, n> &s){
for (int i = 0; i < s.count; i++)
data[i] = s.data[i];
count = s.count;
return *this;
}
int GetCount(){ return count; }
int IsEmpty(){ return count == 0; }
void Empty(){ count = 0; }
void Add(T a){
if (count != n - 1)
data[count++] = a;
}
bool Search(T a){
for (int i = 0; i < count; i++)
if (data[i] == a)
return true;
return false;
}
Set<T,n> Delete(T a)
{
for (int i = 0; i < count;i++)
{
if (a == data[i])
{
for (int j = i; j < count - 1; j++)
data[j] = data[j + 1];
count--;
return *this;
}
}
}
friend Set<T, n> operator+(Set<T, n> &s1, Set<T, n> &s2);
friend Set<T, n> operator-(Set<T, n> &s1, Set<T, n> &s2);
friend Set<T, n> operator*(Set<T, n> &s1, Set<T, n> &s2);
friend bool operator>(Set<T, n> &s1, Set<T, n> &s2);
friend bool operator<(Set<T, n> &s1, Set<T, n> &s2);
private:
T data[n];
int count;
};
//====================
#include "Set.h"
template<class T,int n>
Set<T, n> operator+(Set<T, n> &s1, Set<T, n> &s2)
{
Set<T, n> res = s1;
for (int i = 0; i < s2.count; i++)
if (!res.Search(s2.data[i])) res.Add(s2.data[i]);
return res;
}
template<class T, int n>
Set<T, n> operator-(Set<T, n> &s1, Set<T, n> &s2)
{
Set<T, n> res = s1;
for (int i = 0; i < s2.count; i++)
{
if (res.Search(s2.data[i]))
res.Delete(s2.data[i]);
else
res.Add(s2.data[i]);
}
return res;
}
template<class T,int n>
Set<T, n> operator*(Set<T, n> &s1, Set<T, n> &s2)
{
Set<T, n> res;
for (int i = 0; i < s1.count; i++)
if (s2.Search(s1.data[i])) res.Add(s1.data[i]);
return res;
}
template<class T,int n>
bool operator>(Set<T, n> &s1, Set<T, n> &s2)
{
for (int i = 0; i < s2.count; i++)
if (!s1.Search(s2.data[i])) return false;
return true;
}
template<class T, int n>
bool operator<(Set<T, n> &s1, Set<T, n> &s2)
{
if (s2>s1)
return true;
return false;
}
Khi e chia định nghĩa hàm ngoài khuôn mẫu lớp thì lỗi : e định nghĩa bên trong thì không sao cả
E thấy có lỗi gì đâu ạ? E xem giúp e với!
Vâng đúng a ạ! Tại vì e thử include thêm file.cpp thì lại không bị sao.
E thường hay chia ra 2 để dễ quản lý ->làm với khuôn mẫu lớp thì e thấy sinh ra điều này 

83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?