我有两个二进制运算符加重的问题"<"one_answers" ==",但首先我必须解释我的计划。因此,我计划使用自己的地图创建模板类频击。地图应采用变量并计数这些变量的频率数量。在来源中,我想添加模板类型的数据类型,例如颜色向量。我的颜色是三个变量的结构,为红绿色。早些时候,我有PPM的类PPM,该类正在加载构造函数中的图像。图像类是PPM的基类。这是代码。
标题包括:image.h,ppm.h,color.h
#include <iostream>
#include "color.h"
#include "freqcounter.h"
#include "ppm.h"
int main()
{
freqcounter<color> color_count;
//
Image *ob;
ob = new ppm("Lenna.ppm");
std::vector<color> test = ob->pixel;
for (auto i = test.begin(); i != test.end(); i++)
{
color_count.addData(*i);
}
color_count.create();
system("Pause");
}
fyseycounter.h:
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <iomanip>
#include <set>
#include "Image.h"
#include "ppm.h"
#include "color.h"
template <typename T>
class freqcounter
{
public:
freqcounter();
~freqcounter();
unsigned int counter;
std::vector<T> vec;
std::vector<color> pixels;
void addData(T num);
void create();
};
template<typename T>
freqcounter<T>::freqcounter()
{
}
template<typename T>
freqcounter<T>::~freqcounter()
{
}
template<typename T>
void freqcounter<T>::addData(T num)
{
vec.push_back(num);
}
template<typename T>
void freqcounter<T>::create()
{
std::string name;
std::cout << "enter name of file" << std::endl;
std::cin >> name;
std::ofstream ofile;
ofile.open(name, std::ofstream::out);
std::set<T>vec_of_uniq(vec.begin(), vec.end());
for (const auto& elem : vec_of_uniq)
{
licznik = std::count(vec.begin(), vec.end(), elem);
std::cout<< elem << " " << counter << "n";
}
}
color.h:
#ifndef COLOR_H
#define COLOR_H
#include <tuple>
#include <iostream>
struct color {
bool operator<(const color & right) const
{
return std::tie(R, G, B) < std::tie(right.R, right.G, right.B);
}
unsigned int R;
unsigned int G;
unsigned int B;
};
std::ostream& operator<<(std::ostream& ob, const color& ob1)
{
ob << ob1.R << ob1.G << ob1.B;
return ob;
}
inline bool operator == (const color& left, const color& right)
{
return std::tie(left.R, left.G, left.B) == std::tie(right.R, right.G, right.B);
}
ppm.h:
#ifndef PPM_H
#define PPM_H
#include "image.h"
class ppm : public image
public:
ppm();
ppm(std::string name );
~ppm();
};
#endif
ppm.cpp:
#include "ppm.h"
ppm::ppm():image()
{
}
ppm::ppm(ppm & copy) : image(copy)
{
}
ppm::~ppm()
{
}
ppm::ppm(std::string filename)
{
// std::string hash;
std::ifstream file;
file.open(filename, std::ios::in | std::ios::out);
if (plik.good())
{
file >> head;
file >> height;
file >> width;
file >> balance;
for (int i = 0; i < (width*height); i++)
{
file >> structs.R;
file >> structs.G;
file >> structs.B;
pixel.push_back(struts);
}
plik.close();
}
else
{
std::cerr << "no file " << std::endl;
}
}
image.h//
#ifndef IMAGE_H
#define IMAGE_H
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <set>
#include <exception>
#include "color.h"
class image
{
public:
image();
image(const image©);
image& operator=(const image &M1);
virtual ~image();
color getColor( int i, int j);
std::vector <color> pixel;
protected:
color structs;
int height, width, balance;
std::string head;
};
#endif
我的方法。用于计数类型及其频率在创建函数中定义为
std::set<T>vec_of_uniq(vec.begin(), vec.end());
for (const auto& elem : vec_of_uniq)
{
counter = std::count(vec.begin(), vec.end(), elem); // err code 2679 if
// comment next line
ofile<< elem << " " << counter << "n"; // err code 2678
}
}
和" =="操作员和"&lt;&lt;"为了保存以归档opearator是不可见的。(Visual Studio中的c2679错误代码加剧),但是我已经定义了这两个运算符。h
如何解决这个问题呢 ?我的解决方案适用于简单类型作为字符串,字符和ints,但在lambda count :: method的lambda表达方面存在问题。顺便一提。如果这些方法不是那么好,那么您的解决问题是什么?主要问题是创建自己的模板映射,该映射计算出现的数量并保存到文件中(作为源.CPP中的最佳,但使用此方法,我不知道如何移动它。谢谢所有响应。
当然,您对color
结构的operator<
有问题。如果要使用类型创建set<T>
,则应将operator<
定义为const成员。
bool color::operator<(const color & right) const // <---
{
return std::tie(R, G, B) < std::tie(right.R, right.G, right.B);
}
真的很简单,就像您的#include Guards在Image.hpp中是错误的吗?
您将其标记为Image.cpp,但显然是标题。您正在测试图像标头内的color_h值,这似乎是错误的。
但是,在您发布的代码中,这是实际颜色中的所有大写字母。但是,如果您编辑了任何发布的内容,恕我直言,这太多了,那么很难说
您写了这篇文章:
image.cpp:
#ifndef color_H
#define color_H
...
class image
{
public:
假设您实际上拼写相同(这是一个猜测),那么当您包含color.h时,color_h已定义,并且您不会看到标头的内容。