得空看一下 2021-01-07 21:30 采纳率: 33.3%
浏览 128

C++的类定义在函数后面,在函数前对类声明,还是会报错?

问题描述

听了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;
}
  • 写回答

1条回答 默认 最新

  • 辛潇 2021-01-07 22:20
    关注

    前置声明只能作为指针或引用,不能定义类的对象,也不能调用对象中的方法

    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算