gpt4 book ai didi

C++ I/O 流为 3 个不同的类重载 : How to implement friend overload,,相同 header 中的相同 4 个 I/O 函数?

转载 作者:太空宇宙 更新时间:2023-11-04 12:59:05 25 4
gpt4 key购买 nike

我有一个简单的 OpenGL 项目。它的结构非常简单:

图.h(下图)->TFigureMatrix.h(操作图形的模板)->Cluster.h(类似于组织所有大功能的子菜单)-> Source.cpp(控制台菜单)

图.h:

#pragma once

#define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>
#include <glad/glad.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>

#include <vector>
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
#include <time.h>
#include <iomanip>

#define RND1 (GLfloat)((rand() % 101)/500.)
#define RND2 (GLfloat)((rand() % 151-75)/100.)
#define RND3 (GLfloat)(rand() % 361)
#define SQR(x) ((x)*(x))
#define Len2(i, j) sqrt(SQR(Fig[i*3] - Fig[j*3]) + SQR(Fig[1+i*3] - Fig[1+j*3]))
#define MIN_val 0.005

class Square
{
GLfloat Fig[18] = {
0.0f,0.0f,0.0f,
0.0f,0.0f,0.0f,
0.0f,0.0f,0.0f,
0.0f,0.0f,0.0f,

0.0f,0.0f,0.0f,
0.0f,0.0f,0.0f
};
public:
inline GLfloat S() { return SQR(Fig[3]); };
inline GLfloat P() { return Fig[3] * 4; };
inline GLfloat D() {
return sqrt(
SQR((Fig[0] + Fig[3] + Fig[6] + Fig[9]) / 4. + Fig[12]) +
SQR((Fig[1] + Fig[4] + Fig[7] + Fig[10]) / 4. + Fig[13]) +
SQR((Fig[2] + Fig[5] + Fig[8] + Fig[11]) / 4. + Fig[14]));
};
inline GLfloat* GetFig() { return Fig; };
void Rnd() {
memset(Fig, 0, sizeof(Fig));
Fig[3] = RND1;
Fig[12] = RND2; Fig[13] = RND2; Fig[14] = RND2;
Fig[15] = RND3; Fig[16] = RND3; Fig[17] = RND3;
Fig[7] = Fig[3]; Fig[9] = Fig[3]; Fig[10] = Fig[3];;
};

friend std::istream &operator >> (std::istream &, Square &);
friend std::ostream &operator << (std::ostream &, Square &);
friend std::ifstream &operator >> (std::ifstream &, Square &);
friend std::ofstream &operator << (std::ofstream &, Square &);
};
class Rectang
{
GLfloat Fig[18] = {
0.0f,0.0f,0.0f,
0.0f,0.0f,0.0f,
0.0f,0.0f,0.0f,
0.0f,0.0f,0.0f,
0.0f,0.0f,0.0f,
0.0f,0.0f,0.0f
};
public:
inline GLfloat S() { return Fig[3] * Fig[7]; };
inline GLfloat P() { return (Fig[3] + Fig[7]) * 2; };
inline GLfloat D() {
return sqrt(
SQR((Fig[0] + Fig[3] + Fig[6] + Fig[9]) / 4. + Fig[12]) +
SQR((Fig[1] + Fig[4] + Fig[7] + Fig[10]) / 4. + Fig[13]) +
SQR((Fig[2] + Fig[5] + Fig[8] + Fig[11]) / 4. + Fig[14]));
};
inline GLfloat* GetFig() { return Fig; };
void Rnd() {
memset(Fig, 0, sizeof(Fig));
Fig[3] = RND1; Fig[7] = RND1;
Fig[12] = RND2; Fig[13] = RND2; Fig[14] = RND2;
Fig[15] = RND3; Fig[16] = RND3; Fig[17] = RND3;
Fig[9] = Fig[3]; Fig[10] = Fig[7];
};
friend std::istream &operator >> (std::istream &, Rectang &);
friend std::ostream &operator << (std::ostream &, Rectang &);
friend std::ifstream &operator >> (std::ifstream &, Rectang &);
friend std::ofstream &operator << (std::ofstream &, Rectang &);
};
class Triangle
{
GLfloat Fig[15] = {
0.0f,0.0f,0.0f,
0.0f,0.0f,0.0f,
0.0f,0.0f,0.0f,
0.0f,0.0f,0.0f,
0.0f,0.0f,0.0f
};
public:
inline GLfloat P() { return Len2(0, 1) + Len2(0, 2) + Len2(1, 2); };
inline GLfloat S() { GLfloat hP = P() / 2.0f; return sqrt(hP*(hP - Len2(0, 1))*(hP - Len2(0, 2))*(hP - Len2(1, 2))); };
inline GLfloat D() {
return sqrt(
SQR((Fig[0] + Fig[3] + Fig[6]) / 3. + Fig[9]) +
SQR((Fig[1] + Fig[4] + Fig[7]) / 3. + Fig[10]) +
SQR((Fig[2] + Fig[5] + Fig[8]) / 3. + Fig[11]));
};
inline GLfloat* GetFig() { return Fig; };
void Rnd() {
memset(Fig, 0, sizeof(Fig));
Fig[3] = RND1; Fig[6] = RND1; Fig[7] = RND1;
Fig[9] = RND2; Fig[10] = RND2; Fig[11] = RND2;
Fig[12] = RND3; Fig[13] = RND3; Fig[14] = RND3;
};
friend std::istream &operator >> (std::istream &, Triangle &);
friend std::ostream &operator << (std::ostream &, Triangle &);
friend std::ifstream &operator >> (std::ifstream &, Triangle &);
friend std::ofstream &operator << (std::ofstream &, Triangle &);
};

std::istream &operator >> (std::istream &stream, Square &obj) {
memset((void*)obj.Fig, 0, sizeof(obj.Fig));
stream >> obj.Fig[3] >> obj.Fig[7] >> obj.Fig[9] >> obj.Fig[10]
>> obj.Fig[12] >> obj.Fig[13] >> obj.Fig[14]
>> obj.Fig[15] >> obj.Fig[16] >> obj.Fig[17];
return stream;
};
std::ostream &operator << (std::ostream &stream, Square &obj) {
stream << "\n/(x: " << obj.Fig[3] << ")";
return stream;
};
std::ifstream &operator >> (std::ifstream &stream, Square &obj) {
char delim;
memset((void*)obj.Fig, 0, sizeof(obj.Fig));
stream >>
delim >> obj.Fig[3] >>
delim >> obj.Fig[7] >>
delim >> obj.Fig[9] >>
delim >> obj.Fig[10] >>
delim >> obj.Fig[12] >> obj.Fig[13] >> obj.Fig[14] >>
delim >> obj.Fig[15] >> obj.Fig[16] >> obj.Fig[17];
return stream;
};
std::ofstream &operator << (std::ofstream &stream, Square &obj) {
stream << "\n" << std::fixed << std::setprecision(3)
<< "\nx " << obj.Fig[3] << "\ny " << obj.Fig[7]
<< "\nx " << obj.Fig[9] << "\ny " << obj.Fig[10] << std::setprecision(2)
<< "\nm " << obj.Fig[12] << " " << obj.Fig[13] << " " << obj.Fig[14]
<< "\nr " << obj.Fig[15] << " " << obj.Fig[16] << " " << obj.Fig[17];
return stream;
};

std::istream &operator >> (std::istream &stream, Rectang &obj) {
memset((void*)obj.Fig, 0, sizeof(obj.Fig));
stream >> obj.Fig[3] >> obj.Fig[7] >> obj.Fig[9] >> obj.Fig[10]
>> obj.Fig[12] >> obj.Fig[13] >> obj.Fig[14]
>> obj.Fig[15] >> obj.Fig[16] >> obj.Fig[17];
return stream;
};
std::ostream &operator << (std::ostream &stream, Rectang &obj) {
stream << "\n/(x: " << obj.Fig[3] << "|y: " << obj.Fig[7] << ")";
return stream;
};
std::ifstream &operator >> (std::ifstream &stream, Rectang &obj) {
char delim;
memset((void*)obj.Fig, 0, sizeof(obj.Fig));
stream >>
delim >> obj.Fig[3] >>
delim >> obj.Fig[7] >>
delim >> obj.Fig[9] >>
delim >> obj.Fig[10] >>
delim >> obj.Fig[12] >> obj.Fig[13] >> obj.Fig[14] >>
delim >> obj.Fig[15] >> obj.Fig[16] >> obj.Fig[17];
return stream;
};
std::ofstream &operator << (std::ofstream &stream, Rectang &obj) {
stream << "\n" << std::fixed << std::setprecision(3)
<< "\nx " << obj.Fig[3] << "\ny " << obj.Fig[7]
<< "\nx " << obj.Fig[9] << "\ny " << obj.Fig[10] << std::setprecision(2)
<< "\nm " << obj.Fig[12] << " " << obj.Fig[13] << " " << obj.Fig[14]
<< "\nr " << obj.Fig[15] << " " << obj.Fig[16] << " " << obj.Fig[17];
return stream;
};

std::istream &operator >> (std::istream &stream, Triangle &obj) {
memset((void*)obj.Fig, 0, sizeof(obj.Fig));
stream >> obj.Fig[3] >> obj.Fig[6] >> obj.Fig[7]
>> obj.Fig[9] >> obj.Fig[10] >> obj.Fig[11]
>> obj.Fig[12] >> obj.Fig[13] >> obj.Fig[14];
return stream;
};
std::ostream &operator << (std::ostream &stream, Triangle &obj) {
stream << "\n/(x: " << obj.Fig[3] << "|x: " << obj.Fig[6] << " y: " << obj.Fig[7] << ")";
return stream;
};
std::ifstream &operator >> (std::ifstream &stream, Triangle &obj) {
char delim;
memset((void*)obj.Fig, 0, sizeof(obj.Fig));
stream >>
delim >> obj.Fig[3] >>
delim >> obj.Fig[6] >>
delim >> obj.Fig[7] >>
delim >> obj.Fig[9] >> obj.Fig[10] >> obj.Fig[11] >>
delim >> obj.Fig[12] >> obj.Fig[13] >> obj.Fig[14];
return stream;
};
std::ofstream &operator << (std::ofstream &stream, Triangle &obj) {
stream << "\n" << std::fixed << std::setprecision(3)
<< "\nx " << obj.Fig[3] << "\nx " << obj.Fig[6]
<< "\ny " << obj.Fig[7] << std::setprecision(2)
<< "\nm " << obj.Fig[9] << " " << obj.Fig[10] << " " << obj.Fig[11]
<< "\nr " << obj.Fig[12] << " " << obj.Fig[13] << " " << obj.Fig[14];
return stream;
};

目的是将其包含在另一个模板类中,该模板类将在这里类似地处理所有不同的数字。问题是,我似乎无法在一个 header 中创建 2 个或更多类似的 friend 重载:

std::istream &operator >> (std::istream &stream, Square &obj)
std::istream &operator >> (std::istream &stream, Rectang &obj)

例如,在 Figure.h 中,他指出了 13 个类似的错误,例如:

LNK2005 "class std::basic_ifstream<char,struct std::char_traits<char> > & __cdecl operator>>(class std::basic_ifstream<char,struct std::char_traits<char> > &,class Rectang &)" (??5@YAAAV?$basic_ifstream@DU?$char_traits@D@std@@@std@@AAV01@AAVRectang@@@Z) already defined in Cluster.obj

如果我将它们放在类中,则显然看不到它们,因为它们一定是 friend 。如果我在它说的所有图形 obj 之前添加 const,则“>>/<< const whatever_type”没有重载。

我该如何解决这个问题?(如果有任何错误,或者如果我的问题已经被问过,但还没有找到,我很抱歉)

最佳答案

您正在标题中定义函数。这定义了包含该 header 的每个 .cpp 文件中的函数。链接器看到了所有这些函数,但并不知道它们是同一个函数,并让您出错。

添加内联关键字作为快速选择。

inline std::ofstream &operator << (std::ofstream &stream, Triangle &obj) {
...
}

或者在头文件中声明函数并在单个 .cpp 文件中定义它们。

关于C++ I/O 流为 3 个不同的类重载 : How to implement friend overload,,相同 header 中的相同 4 个 I/O 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45073232/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com