我试着让这个程序运行但我无法弄清楚这个错误的错误。
我有这5个文件,我在candidateType.cpp
中得到错误candidatetype.h
#ifndef H_candidateType
#define H_candidateType
#include <string>
#include <iostream>
#include "personType.h"
const int NO_OF_REGIONS = 4;
class candidateType: public personType
{
public:
const candidateType& operator=(const candidateType&);
const candidateType& operator=(const personType&);
void updateVotesByRegion(int region, int votes);
void setVotes(int region, int votes);
void calculateTotalVotes();
int getTotalVotes() const;
void printData() const;
candidateType();
bool operator==(const candidateType& right) const;
bool operator!=(const candidateType& right) const;
bool operator<=(const candidateType& right) const;
bool operator<(const candidateType& right) const;
bool operator>=(const candidateType& right) const;
bool operator>(const candidateType& right) const;
private:
int votesByRegion[NO_OF_REGIONS];
int totalVotes;
};
#endif
candidatetype.cpp
#include "candidateType.h"
void candidateType::setVotes(int region, int votes)
{
votesByRegion[region - 1] = votes;
}
void candidateType::updateVotesByRegion(int region, int votes)
{
votesByRegion[region - 1] = votesByRegion[region - 1] + votes;
}
void candidateType::calculateTotalVotes()
{
int i;
totalVotes = 0;
for (i = 0; i < NO_OF_REGIONS; i++)
totalVotes += votesByRegion[i];
}
int candidateType::getTotalVotes() const
{
return totalVotes;
}
void candidateType::printData() const
{
cout << left
<< setw(8) << firstName << " "
<< setw(8) << lastName << " ";
cout << right;
for (int i = 0; i < NO_OF_REGIONS; i++)
cout << setw(8) << votesByRegion[i] << " ";
cout << setw(7) << totalVotes << endl;
}
candidateType::candidateType()
{
for (int i = 0; i < NO_OF_REGIONS; i++)
votesByRegion[i] = 0;
totalVotes = 0;
}
bool candidateType::operator==(const candidateType& right) const
{
return (firstName == right.firstName && lastName == right.lastName);
}
bool candidateType::operator!=(const candidateType& right) const
{
return (firstName != right.firstName || lastName != right.lastName);
}
bool candidateType::operator<=(const candidateType& right) const
{
return (lastName <= right.lastName || (lastName == right.lastName
&& firstName <= right.firstName));
}
bool candidateType::operator<(const candidateType& right) const
{
return (lastName < right.lastName || (lastName == right.lastName
&& firstName < right.firstName));
}
bool candidateType::operator>=(const candidateType& right) const
{
return (lastName >= right.lastName || (lastName == right.lastName
&& firstName >= right.firstName));
}
bool candidateType::operator>(const candidateType& right) const
{
return (lastName > right.lastName || (lastName == right.lastName
&& firstName > right.firstName));
}
const candidateType& candidateType::operator=(const candidateType& right)
{
if (this != &right)
{
firstName = right.firstName;
lastName = right.lastName;
for (int i = 0; i < NO_OF_REGIONS; i++)
votesByRegion[i] = right.votesByRegion[i];
totalVotes = right.totalVotes;
}
return *this;
}
const candidateType& candidateType::operator=(const personType& right)
{
firstName = right.getFirstName();
lastName = right.getLastName();
return *this;
}
成员函数中的void candidateType :: printData()const | 31 |错误:未在此范围中声明'setw' || ===构建失败:1个错误,0个警告(0分钟,0秒(秒))=== |
答案 0 :(得分:1)
您需要包含标题<iomanip>
并使用嵌套名称std::setw
答案 1 :(得分:1)
'setw' was not declared
表示您缺少setw
声明。它在名称空间std
中定义,并在<iomanip>
标题中显示。所以只需要包含这个并用
std::setw