问题描述
听了B站的黑马程序员的c++课,链接如下。为什么我在.cpp中把类定义在函数后面,函数前对类进行声明,会报错?
https://www.bilibili.com/video/BV1et411b73Z?p=184
下面是.hpp文件代码:
#pragma once
#include<iostream>
using namespace std;
//#include<string>
template<class T>
class myArray{
public:
//构造函数
myArray(int capacity){
//cout<<"构造函数"<<endl;
this->m_Capacity=capacity;
this->m_size=0;
pAddress=new T[this->m_Capacity];
}
//拷贝构造函数
myArray(const myArray &arr){
//cout<<"拷贝构造函数"<<endl;
this->m_Capacity=arr.m_Capacity;
this->m_size=arr.m_size;
this->pAddress=new T[arr.m_Capacity];
for (int i = 0; i < arr.m_size; i++)
{
this->pAddress[i]=arr.pAddress[i];
}
}
//重载operator=
myArray& operator=(const myArray &arr){
//cout<<"重载operator="<<endl;
if (this->pAddress!=NULL)
{
this->m_Capacity=0;
this->m_size=0;
delete [] this->pAddress;
this->pAddress=NULL;
}
this->m_Capacity=arr.m_Capacity;
this->m_size=arr.m_size;
this->pAddress=new T[arr.m_Capacity];
for (int i = 0; i < arr.m_size; i++)
{
this->pAddress[i]=arr.pAddress[i];
}
return *this;
}
//重载[]
T& operator[](int index){
return this->pAddress[index];
}
//尾插
void pushBack(const T & val){
if (this->m_Capacity==this->m_size)
{
cout<<"数组已满"<<endl;
return;
}
this->pAddress[this->m_size]=val;
this->m_size++;
}
//尾删
void PopBack(){
if (this->m_size==0)
{
cout<<"数组为空"<<endl;
return;
}
this->m_size--;
}
//获取容量
int getCapacity(){
return this->m_Capacity;
}
//获取大小
int getSize(){
return this->m_size;
}
//析构函数
~myArray(){
//cout<<"析构函数"<<endl;
if (this->pAddress!=NULL)
{
delete [] this->pAddress;
this->pAddress =NULL;
this->m_Capacity=0;
this->m_size=0;
}
}
private:
int m_Capacity;//容量,数组容量
int m_size;//大小,目前数组中的元素个数
T * pAddress;//指向一个堆空间
};
下面是.cpp文件代码:
#include<iostream>
using namespace std;
#include<string>
#include "myArray.hpp"
class Person;
void printIntArray(myArray<int> &arr){
for (int i = 0; i < arr.getSize(); i++)
{
cout<<arr[i];
}
cout<<endl;
}
void printPersonArray(myArray<Person> &arr)
{
for (int i = 0; i < arr.getSize(); i++)
{
cout<<"姓名:"<<arr[i].m_Name<<" 年龄:"<<arr[i].m_Age<<endl;
}
}
void test(){
myArray<int> array(10);
for (int i = 0; i < array.getCapacity(); i++)
{
array.pushBack(i);
}
printIntArray(array);
cout<<"容量"<<array.getCapacity()<<endl;
cout<<"大小"<<array.getSize()<<endl;
myArray<Person> array1(10);
Person p1("孙悟空",22);
Person p2("猪八戒",25);
Person p3("唐僧",22);
array1.pushBack(p1);
array1.pushBack(p2);
array1.pushBack(p3);
printPersonArray(array1);
}
class Person{
public:
Person(){}
Person(string name,int age){
this->m_Age=age;
this->m_Name=name;
}
string m_Name;
int m_Age;
private:
};
void main(){
test();
system("pause");
}
下面是报错提示:
我看之前的一节课就可以对类声明的操作,这里就不行了?
之前讲全局函数类外实现时就可以对类声明,代码如下:
#include <string>
//2、全局函数配合友元 类外实现 - 先做函数模板声明,下方在做函数模板定义,在做友元
template<class T1, class T2> class Person;
//如果声明了函数模板,可以将实现写到后面,否则需要将实现体写到类的前面让编译器提前看到
//template<class T1, class T2> void printPerson2(Person<T1, T2> & p);
template<class T1, class T2>
void printPerson2(Person<T1, T2> & p)
{
cout << "类外实现 ---- 姓名: " << p.m_Name << " 年龄:" << p.m_Age << endl;
}
template<class T1, class T2>
class Person
{
//1、全局函数配合友元 类内实现
friend void printPerson(Person<T1, T2> & p)
{
cout << "姓名: " << p.m_Name << " 年龄:" << p.m_Age << endl;
}
//全局函数配合友元 类外实现
//加空模板参数列表
friend void printPerson2<>(Person<T1, T2> & p);
public:
Person(T1 name, T2 age)
{
this->m_Name = name;
this->m_Age = age;
}
private:
T1 m_Name;
T2 m_Age;
};
//1、全局函数在类内实现
void test01()
{
Person <string, int >p("Tom", 20);
printPerson(p);
}
//2、全局函数在类外实现
void test02()
{
Person <string, int >p("Jerry", 30);
printPerson2(p);
}
int main() {
//test01();
test02();
system("pause");
return 0;
}