我在 C++ 上做这个项目,我必须返回独特的元素。如果有人可以帮助我解决这个问题这是我整个代码的示例
#include <iostream.h>
class CService {
private:
string m_strSeller;
//other stuff
};
class CServiceAnalizer {
//other stuff
unique function
};
这是我独特的功能的样子
CService unique (const CService& a, const CService& b) {
if(a.m_strSeller==b.m_strSeller) {
return b.m_strSeller;
}
CService result = a.m_strSeller;
while (++a.m_strSeller != b.m_strSeller) {
if (!(*result == *a.m_strSeller)) {
*(++result)=*a.m_strSeller;
}
}
return ++result;
}
主要错误之一是'm_strSeller' : cannot access private member declared in class 'CService'
如果有人可以向我解释究竟是什么让编译器出现这个错误,我该如何修复它们。
These are the errors from the compiler:
--------------------Configuration: Proekt - Win32 Debug--------------------
Compiling...
Proekt.cpp
D:\My Documents\OOP Proekt\Proekt\Proekt.cpp(191) : error C2248: 'm_strSeller' : cannot access private member declared in class 'CService'
D:\My Documents\OOP Proekt\Proekt\Proekt.cpp(17) : see declaration of 'm_strSeller'
D:\My Documents\OOP Proekt\Proekt\Proekt.cpp(191) : error C2248: 'm_strSeller' : cannot access private member declared in class 'CService'
D:\My Documents\OOP Proekt\Proekt\Proekt.cpp(17) : see declaration of 'm_strSeller'
D:\My Documents\OOP Proekt\Proekt\Proekt.cpp(192) : error C2248: 'm_strSeller' : cannot access private member declared in class 'CService'
D:\My Documents\OOP Proekt\Proekt\Proekt.cpp(17) : see declaration of 'm_strSeller'
D:\My Documents\OOP Proekt\Proekt\Proekt.cpp(192) : error C2664: '__thiscall CService::CService(int)' : cannot convert parameter 1 from 'const class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'int'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
D:\My Documents\OOP Proekt\Proekt\Proekt.cpp(195) : error C2248: 'm_strSeller' : cannot access private member declared in class 'CService'
D:\My Documents\OOP Proekt\Proekt\Proekt.cpp(17) : see declaration of 'm_strSeller'
D:\My Documents\OOP Proekt\Proekt\Proekt.cpp(195) : error C2440: 'initializing' : cannot convert from 'const class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'class CService'
No constructor could take the source type, or constructor overload resolution was ambiguous
D:\My Documents\OOP Proekt\Proekt\Proekt.cpp(196) : error C2248: 'm_strSeller' : cannot access private member declared in class 'CService'
D:\My Documents\OOP Proekt\Proekt\Proekt.cpp(17) : see declaration of 'm_strSeller'
D:\My Documents\OOP Proekt\Proekt\Proekt.cpp(196) : error C2675: unary '++' : 'const class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' does not define this operator or a conversion to a type acceptable to the p
redefined operator
D:\My Documents\OOP Proekt\Proekt\Proekt.cpp(196) : error C2248: 'm_strSeller' : cannot access private member declared in class 'CService'
D:\My Documents\OOP Proekt\Proekt\Proekt.cpp(17) : see declaration of 'm_strSeller'
D:\My Documents\OOP Proekt\Proekt\Proekt.cpp(196) : fatal error C1903: unable to recover from previous error(s); stopping compilation
Error executing cl.exe.
Proekt.obj - 10 error(s), 0 warning(s)
我的整个代码:
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <algorithm>
#include <iomanip>
using namespace std;
using std::ostream;
using std::endl;
using std::cout;
class CService
{
private:
string m_strClient;
string m_strSeller;
int m_iMinutes;
public:
CService(int m = 0) // Podrazbirasht se konstruktor
{
m_strClient = "N/A";
m_strSeller = "N/A";
m_iMinutes = m;
}
CService(string c, string s, int m) // Ekspliciten konstruktor
{
m_strClient = c;
m_strSeller = s;
m_iMinutes = m;
}
CService(const CService &obj) // Copy konstruktor
{
m_strClient = obj.m_strClient;
m_strSeller = obj.m_strSeller;
m_iMinutes = obj.m_iMinutes;
}
string GetClient() const
{
return m_strClient;
}
string GetSeller() const
{
return m_strSeller;
}
int GetMinutes() const
{
return m_iMinutes;
}
void SetClient (string c)
{
m_strClient = c;
}
void SetSeller (string s)
{
m_strSeller = s;
}
void SetMinutes (int m)
{
m_iMinutes = m;
}
CService operator =(CService obj)
{
m_strClient = obj.m_strClient;
m_strSeller = obj.m_strSeller;
m_iMinutes = obj.m_iMinutes;
return *this;
}
bool operator < (const CService &obj) const
{
return m_iMinutes < obj.m_iMinutes;
}
CService operator +(const CService &obj) const
{
return CService(m_iMinutes + obj.m_iMinutes);
}
friend ostream& operator <<(ostream& os, CService &obj);
friend istream& operator>>(istream& is, CService &obj);
};
ostream& operator<<(ostream& os, const CService &obj) {
os<<obj.GetClient()<<obj.GetSeller()<<obj.GetMinutes()<<endl;
return os;
}
istream& operator>>(istream& is, CService &obj) {
string tmp_strClient;
string tmp_strSeller;
int tmp_iMinutes;
is>>tmp_strClient>>tmp_strSeller>>tmp_iMinutes;
obj.SetClient(tmp_strClient);
obj.SetSeller(tmp_strSeller);
obj.SetMinutes(tmp_iMinutes);
return is;
}
class CServiceAnalizer {
private:
vector<CService>m_vData;
void add(CService seller) {
m_vData.push_back(seller);
}
//13.05.2013
public:
CService getSellerAt(int i)
{
return m_vData[i];
}
long getSellerCount()
{
return m_vData.size();
}
CServiceAnalizer()
{
ifstream fs;
fs.open("test.txt");
if(!fs.is_open()) cout<<"error opening file!\n";
CService seller;
while(!fs.eof())
{
fs>>seller;
add(seller);
}
}
CServiceAnalizer(const string& strFileName)
{
ifstream fs;
fs.open(strFileName.c_str());
if(!fs.is_open()) cout<<"error opening file!\n";
CService seller;
while(!fs.eof())
{
fs>>seller;
add(seller);
}
}
void Sort()
{
sort(m_vData.begin(),m_vData.end());
}
double calcMean()
{
double sum=0;
for (int i=0;i<m_vData.size();i++)
{
sum+=m_vData[i].GetMinutes();
}
return sum/m_vData.size();
}
vector<int> calcNums(int iR1,int iR2,int iR3,int iR4,int iR5)
{
vector<int> resultVector;
for (int i=0;i<4;i++)
{
resultVector.push_back(0);
}
for (i=0;i<m_vData.size();i++)
{
if(m_vData[i].GetMinutes()>=iR1&&m_vData[i].GetMinutes()<iR2) resultVector[0]++;//[iR1-iR2)
if(m_vData[i].GetMinutes()>=iR2&&m_vData[i].GetMinutes()<iR3) resultVector[1]++;//[iR2-iR3)
if(m_vData[i].GetMinutes()>=iR3&&m_vData[i].GetMinutes()<iR4) resultVector[2]++;//[iR3-iR4)
if(m_vData[i].GetMinutes()>=iR4&&m_vData[i].GetMinutes()<iR5) resultVector[3]++;//[iR4-iR5)
}
return resultVector;
}
CService unique (const CService& a, const CService& b) {
if(a.m_strSeller==b.m_strSeller) {
return b.m_strSeller;
}
CService result = a.m_strSeller;
while (++a.m_strSeller != b.m_strSeller) {
if (!(*result == *a.m_strSeller)) {
*(++result)=*a.m_strSeller;
}
}
return ++result;
}
};
ostream& operator<<(ostream& os, CServiceAnalizer &obj)
{
for (int i=0;i<obj.getSellerCount();i++)
{
cout<<obj.getSellerAt(i);
}
return os;
}
int main()
{
CServiceAnalizer myAnalyzer;
//myAnalyzer.Sort();
cout<<"Client"<<"Seller"<<"Minutes"<<endl;
cout<<"----------------------------"<<endl;
cout<<myAnalyzer;
cout<<"CalcMean result:"<<myAnalyzer.calcMean()<<endl;
vector<int>myCalcNums = myAnalyzer.calcNums(1,20,50,80,100);
cout<<"CalcNums result:"<<myCalcNums[0]<<","<<myCalcNums[1]<<","<<myCalcNums[2]<<","<<myCalcNums[3]<<endl;
system("pause");
return 0;
}