[英]How to fix “invalid use of incomplete type” in c++
我想通过使用一个类中的方法在2个类之间建立友谊。 即使我研究了不同的教程和书籍,也无法使它起作用。
编辑 ::它可以在一个文件中工作,但是我想在单独的文件中做-不幸的是不能做:
Tbase_in_memory.h:
#ifndef FRENDY_TBASE_IN_MEMORY_H
#define FRENDY_TBASE_IN_MEMORY_H
#include <iostream>
#include <string>
#include <fstream>
class base;
class Tbase_in_memory
{
public:
Tbase_in_memory(int = 2);
~Tbase_in_memory();
void read_to_arrays(base & b);
private:
std::string *name;
double *price_tag;
int *code;
char *type;
int _size;
};
#endif
Tbase_in_memory.cpp:
#include "Tbase_in_memory.h"
using namespace std;
class base;
Tbase_in_memory::Tbase_in_memory(int s)
{
_size = s;
name = new string[_size];
price_tag = new double[_size];
code = new int[_size];
type = new char[_size];
}
Tbase_in_memory::~Tbase_in_memory()
{
delete[] name;
delete[] price_tag;
delete[] code;
delete[] type;
}
void Tbase_in_memory::read_to_arrays(base & b)
{
string line;
while (getline(b.file, line)) {
cout << line;
}
}
base.h:
#ifndef FRENDY_BASE_H
#define FRENDY_BASE_H
#include <iostream>
#include <string>
#include <fstream>
#include "Tbase_in_memory.h"
class base
{
public:
base(std::string = "...");
~base();
friend void Tbase_in_memory::read_to_arrays(base & b);
private:
std::fstream file;
std::string f_name;
};
#endif
base.cpp
#include "base.h"
using namespace std;
base::base(string n)
{
f_name = n;
file.open(f_name, ios::in);
if (!file.good()) {
cout << "Error";
cout << string(38, '-');
exit(0);
}
}
base::~base()
{
file.close();
}
#include <iostream>
#include "Tbase_in_memory.h"
#include "base.h"
using namespace std;
int main()
{
base b("/home/Sempron/Desktop/code");
Tbase_in_memory a;
a.read_to_arrays(b);
return 0;
}
我有错误:
"error: invalid use of incomplete type ‘class base’
while (getline(b.file, line)) {".
"forward declaration of ‘class base’
class base;"
当声明friend
引用时,必须存在被赋予友谊访问权限的函数的原型/定义。 固定代码(包括):
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
class base;
class Tbase_in_memory
{
public:
Tbase_in_memory(int = 2);
~Tbase_in_memory();
void read_to_arrays(base & b);
private:
std::string *name;
double *price_tag;
int *code;
char *type;
int _size;
};
class base
{
public:
base(std::string = "...");
~base();
friend void Tbase_in_memory::read_to_arrays(base & b);
private:
std::fstream file;
std::string f_name;
};
Tbase_in_memory::Tbase_in_memory(int s)
{
_size = s;
name = new string[_size];
price_tag = new double[_size];
code = new int[_size];
type = new char[_size];
}
Tbase_in_memory::~Tbase_in_memory()
{
delete[] name;
delete[] price_tag;
delete[] code;
delete[] type;
}
void Tbase_in_memory::read_to_arrays(base & b)
{
string line;
while (getline(b.file, line)) {
cout << line;
}
}
base::base(string n)
{
f_name = n;
file.open(f_name, ios::in);
if (!file.good()) {
cout << "Error";
cout << string(38, '-');
exit(0);
}
}
base::~base()
{
file.close();
}
int main()
{
base b("/home/Sempron/Desktop/code");
Tbase_in_memory a;
a.read_to_arrays(b);
return 0;
}
理想情况下,这两个类将在不同的头文件中声明,因此您必须仔细观察include顺序。
在文件Tbase_in_memory.cpp
,还需要包含base.h
然后,您可以在cpp文件中删除前向声明。
#include "Tbase_in_memory.h"
#include "base.h"
using namespace std;
Tbase_in_memory::Tbase_in_memory(int s)
{
//...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.