下面我有2个.cpp和1个.h文件。该程序设置并实现了一个用于处理距离的自定义类。当我跑步时,我得到了这个错误,我看了几个小时,但无法弄清楚它是什么。 distance.cpp顺利编译,但是,当我运行main.cpp时,我得到以下错误:
Compiler: Default compiler
Executing g++.exe...
g++.exe "C:\Users\Himalaya\Desktop\C++\cs1600\distance_class\main.cpp" -o "C:\Users\Himalaya\Desktop\C++\cs1600\distance_class\main.exe" -I"C:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include" -I"C:\Dev-Cpp\include\c++\3.4.2\backward" -I"C:\Dev-Cpp\include\c++\3.4.2\mingw32" -I"C:\Dev-Cpp\include\c++\3.4.2" -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib"
C:\Users\Himalaya\Desktop\C++\cs1600\distance_class\main.cpp: In function `void addDistances(Distance, Distance)':
C:\Users\Himalaya\Desktop\C++\cs1600\distance_class\main.cpp:92: error: no matching function for call to `Distance::Distance(Distance)'
C:\Users\Himalaya\Desktop\C++\cs1600\distance_class\/distance.h:20: note: candidates are: Distance::Distance(Distance&)
C:\Users\Himalaya\Desktop\C++\cs1600\distance_class\main.cpp: In function `void subtractDistances(Distance, Distance)':
C:\Users\Himalaya\Desktop\C++\cs1600\distance_class\main.cpp:102: error: no matching function for call to `Distance::Distance(Distance)'
C:\Users\Himalaya\Desktop\C++\cs1600\distance_class\/distance.h:20: note: candidates are: Distance::Distance(Distance&)
C:\Users\Himalaya\Desktop\C++\cs1600\distance_class\main.cpp: In function `void sumDistanceArray(Distance*)':
C:\Users\Himalaya\Desktop\C++\cs1600\distance_class\main.cpp:116: error: no matching function for call to `Distance::Distance(Distance)'
C:\Users\Himalaya\Desktop\C++\cs1600\distance_class\/distance.h:20: note: candidates are: Distance::Distance(Distance&)
C:\Users\Himalaya\Desktop\C++\cs1600\distance_class\main.cpp: In function `void plotDistanceArray(Distance*)':
C:\Users\Himalaya\Desktop\C++\cs1600\distance_class\main.cpp:148: warning: converting to `int' from `double'
Execution terminated
这是我的距离。
#ifndef DISTANCE_H
#define DISTANCE_H
#include <cmath>
#include <sstream>
#include <string>
using namespace std;
class Distance
{
private:
double inches;
int feet;
public:
Distance();
Distance(int ft, double in);
Distance(Distance &dist);
void setInches(double in);
double getInches();
void setFeet(int ft);
int getFeet();
bool operator==(Distance &dist);
Distance operator+(Distance &dist);
Distance operator-(Distance &dist);
string showDistance();
};
#endif // DISTANCE_H
这是distance.cpp
#include <cmath>
#include <sstream>
#include <string>
#include "distance.h"
using namespace std;
Distance::Distance()
{
feet = 0;
inches = 0;
}
Distance::Distance(int ft, double in)
{
this->feet = ft;
if(in >= 12)
{
while(in >= 12) // pull as many feet as possible out of inches
{
this->feet += 1;
in -= 12;
}
this->inches = in; // store remainder
}
else
{
this->inches = in;
}
}
Distance::Distance(Distance &dist)
{
this->feet = dist.feet;
this->inches = dist.inches;
}
void Distance::setInches(double in)
{
if(in >= 12)
{
while(in >= 12) // pull as many feet as possible out of inches
{
this->feet += 1;
in -= 12;
}
this->inches = in; // store remainder
}
else
{
this->inches = in;
}
}
double Distance::getInches()
{
return this->inches;
}
void Distance::setFeet(int ft)
{
this->feet = ft;
}
int Distance::getFeet()
{
return this->feet;
}
bool Distance::operator==(Distance &dist)
{
if((this->feet == dist.feet) && (this->inches == dist.inches))
{
return true;
}
else
{
return false;
}
}
Distance Distance::operator+(Distance &dist)
{
Distance dist_return;
double total_inches = 0;
dist_return.feet = this->feet + dist.feet;
total_inches = this->inches + dist.inches;
if(total_inches >= 12)
{
while(total_inches >= 12) // pull as many feet as possible out of inches
{
dist_return.feet += 1;
total_inches -= 12;
}
dist_return.inches = total_inches;
}
else
{
dist_return.inches = this->inches + dist.inches;
}
return dist_return;
}
Distance Distance::operator-(Distance &dist)
{
Distance dist_return;
int temp_feet = this->getFeet(); // copy "this" so we can do math and show properly
double temp_inches = this->getInches();
dist_return.feet = temp_feet - dist.feet;
dist_return.inches = temp_inches - dist.inches;
dist_return.feet = abs(dist_return.feet);
dist_return.inches = abs(dist_return.inches); // distance is never negative, and - means difference, so 12 - 20 = 8
return dist_return;
}
string Distance::showDistance()
{
string str_return = "";
std::ostringstream temp_str_holder;
if(this->feet > 0) // only print feet if we have any
{
temp_str_holder.str(""); // clear out ostringstream
temp_str_holder << this->feet;
str_return += temp_str_holder.str();
if(this->feet == 1)
{
str_return += " foot";
}
else
{
str_return += " feet";
}
}
if(this->inches > 0) // only print inches if we have any
{
if(this->feet > 0)
{
str_return += " "; // if we have both feet and inches, print spacer between terms
}
temp_str_holder.str(""); // clear out ostringstream
temp_str_holder << this->inches;
str_return += temp_str_holder.str();
if(this->inches == 1)
{
str_return += " inch";
}
else
{
str_return += " inches";
}
}
if((this->feet == 0) && (this->inches == 0)) // if 0 feet 0 inches, just say 0 inches
{
str_return = "0 inches";
}
return str_return;
}
和main.cpp
#include <iostream>
#include <iomanip>
#include <string>
#include "distance.h"
using namespace std;
void addDistances(Distance dist1, Distance dist2);
void subtractDistances(Distance dist1, Distance dist2);
void sumDistanceArray(Distance dist_array[]);
void plotDistanceArray(Distance dist_array[]);
void calcRectangle(Distance dist1, Distance dist2);
int main()
{
Distance dist1; // showing initialization using defaults
dist1.setInches(6.7);
Distance dist2(1,7); // showing initialization to a distance
Distance dist3(0,34); // showing the auto-conversion of inches to feet as needed
Distance dist4(dist3); // showing initialization to another distance object's values
dist4.setFeet(4);
Distance dist_array[4] = {dist1, dist2, dist3, dist4};
int menu_entry;
bool menu_exit = false;
do
{
//display menu
cout << endl << endl << endl;
cout << "***** Main Menu *****" << endl;
cout << "1: Add two distances" << endl;
cout << "2: Subtract two distances" << endl;
cout << "3: Sum an array of distances" << endl;
cout << "4: Plot an array of distances" << endl;
cout << "5: Calculate the perimeter and area of a rectangle" << endl;
cout << "6: Exit" << endl;
cout << "Please enter the number of your selection: ";
cin >> menu_entry;
cin.ignore(256,'\n'); // clear input buffer of remaining data
// check entry, warn and promt again if invalid
if ((menu_entry < 1) || (menu_entry > 6))
{
cout << endl;
cout << "Invalid entry! Please try again." << endl;
continue;
}
//choose and run function based on user menu selection
switch (menu_entry)
{
case 1:
addDistances(dist2, dist3);
break;
case 2:
subtractDistances(dist2, dist3);
break;
case 3:
sumDistanceArray(dist_array);
break;
case 4:
plotDistanceArray(dist_array);
break;
case 5:
calcRectangle(dist2, dist3);
break;
case 6:
default:
menu_exit = true; //if we picked 6 then exit
break;
}
}
while(!menu_exit);
return 0;
}
void addDistances(Distance dist1, Distance dist2)
{
Distance dist_total;
dist_total = dist1 + dist2;
cout << endl;
cout << "The sum of " << dist1.showDistance() << " and " << dist2.showDistance() << " is "
<< dist_total.showDistance() << endl;
}
void subtractDistances(Distance dist1, Distance dist2)
{
Distance dist_total;
dist_total = dist1 - dist2;
cout << endl;
cout << "The difference of " << dist1.showDistance() << " and " << dist2.showDistance() << " is "
<< dist_total.showDistance() << endl;
}
void sumDistanceArray(Distance dist_array[])
{
int i;
int num_distances = 4;
Distance dist_total;
for(i=0; i<num_distances; i++)
{
dist_total = dist_total + dist_array[i];
}
cout << endl;
cout << "The sum of: " << endl;
for(i=0; i<num_distances; i++)
{
cout << dist_array[i].showDistance() << endl;
}
cout << "is " << dist_total.showDistance() << endl;
}
void plotDistanceArray(Distance dist_array[])
{
int i;
int j;
double inches;
double bar_percentage; // percentage of total columns bar takes up
int bar_length; // number of columns bar is long
int num_distances = 4;
int num_feet_max = 6; // the max of the graph scale
int num_max_disp_columns = 60; // the number of max columns a bar can take up
cout << endl;
cout << "Plot of an array of distances (6 ft scale): " << endl;
for(i=0; i<num_distances; i++)
{
cout << setw(20) << dist_array[i].showDistance() << " ";
inches = dist_array[i].getInches() + (12 * dist_array[i].getFeet());
bar_percentage = inches / (num_feet_max*12);
bar_length = bar_percentage * num_max_disp_columns; // calculate bar length
for(j=0; j<(bar_length+1); j++)
{
cout << "#"; // draw bar
}
cout << endl;
}
}
void calcRectangle(Distance dist1, Distance dist2)
{
Distance rect_area;
Distance rect_perimeter;
double inches1;
double inches2;
inches1 = dist1.getInches() + (12 * dist1.getFeet());
inches2 = dist2.getInches() + (12 * dist2.getFeet());
rect_area.setInches(inches1 * inches2);
rect_perimeter.setInches((2 * inches1) + (2 * inches2));
cout << endl;
cout << "A rectangle with dimensions " << dist1.showDistance() << " and " << dist2.showDistance() << " has an area of "
<< rect_area.showDistance() << " and a perimeter of " << rect_perimeter.showDistance() << endl;
}
我相信这个解决方案非常容易,但是即使我在互联网上查看同样的问题,这个新手也让我无法这样做。我在这做错了什么?
编辑版
#ifndef DISTANCE_H
#define DISTANCE_H
#include <cmath>
#include <sstream>
#include <string>
using namespace std;
class Distance
{
private:
double inches;
int feet;
public:
Distance();
Distance(int ft, double in);
Distance(const Distance &dist);
void setInches(double in);
double getInches();
void setFeet(int ft);
int getFeet();
bool operator==(const Distance &dist);
Distance operator+(Distance &dist);
Distance operator-(Distance &dist);
string showDistance();
};
#endif // DISTANCE_H
#include <cmath>
#include <sstream>
#include <string>
#include "distance.h"
using namespace std;
Distance::Distance()
{
feet = 0;
inches = 0;
}
Distance::Distance(int ft, double in)
{
this->feet = ft;
if(in >= 12)
{
while(in >= 12) // pull as many feet as possible out of inches
{
this->feet += 1;
in -= 12;
}
this->inches = in; // store remainder
}
else
{
this->inches = in;
}
}
Distance::Distance(const Distance &dist)
{
this->feet = dist.feet;
this->inches = dist.inches;
}
void Distance::setInches(double in)
{
if(in >= 12)
{
while(in >= 12) // pull as many feet as possible out of inches
{
this->feet += 1;
in -= 12;
}
this->inches = in; // store remainder
}
else
{
this->inches = in;
}
}
double Distance::getInches()
{
return this->inches;
}
void Distance::setFeet(int ft)
{
this->feet = ft;
}
int Distance::getFeet()
{
return this->feet;
}
bool Distance::operator==(Distance &dist)
{
if((this->feet == dist.feet) && (this->inches == dist.inches))
{
return true;
}
else
{
return false;
}
}
Distance Distance::operator+(Distance &dist)
{
Distance dist_return;
double total_inches = 0;
dist_return.feet = this->feet + dist.feet;
total_inches = this->inches + dist.inches;
if(total_inches >= 12)
{
while(total_inches >= 12) // pull as many feet as possible out of inches
{
dist_return.feet += 1;
total_inches -= 12;
}
dist_return.inches = total_inches;
}
else
{
dist_return.inches = this->inches + dist.inches;
}
return dist_return;
}
Distance Distance::operator-(Distance &dist)
{
Distance dist_return;
int temp_feet = this->getFeet(); // copy "this" so we can do math and show properly
double temp_inches = this->getInches();
dist_return.feet = temp_feet - dist.feet;
dist_return.inches = temp_inches - dist.inches;
dist_return.feet = abs(dist_return.feet);
dist_return.inches = abs(dist_return.inches); // distance is never negative, and - means difference, so 12 - 20 = 8
return dist_return;
}
string Distance::showDistance()
{
string str_return = "";
std::ostringstream temp_str_holder;
if(this->feet > 0) // only print feet if we have any
{
temp_str_holder.str(""); // clear out ostringstream
temp_str_holder << this->feet;
str_return += temp_str_holder.str();
if(this->feet == 1)
{
str_return += " foot";
}
else
{
str_return += " feet";
}
}
if(this->inches > 0) // only print inches if we have any
{
if(this->feet > 0)
{
str_return += " "; // if we have both feet and inches, print spacer between terms
}
temp_str_holder.str(""); // clear out ostringstream
temp_str_holder << this->inches;
str_return += temp_str_holder.str();
if(this->inches == 1)
{
str_return += " inch";
}
else
{
str_return += " inches";
}
}
if((this->feet == 0) && (this->inches == 0)) // if 0 feet 0 inches, just say 0 inches
{
str_return = "0 inches";
}
return str_return;
}
#include <iostream>
#include <iomanip>
#include <string>
#include "distance.h"
using namespace std;
void addDistances(Distance dist1, Distance dist2);
void subtractDistances(Distance dist1, Distance dist2);
void sumDistanceArray(Distance dist_array[]);
void plotDistanceArray(Distance dist_array[]);
void calcRectangle(Distance dist1, Distance dist2);
int main()
{
Distance dist1; // showing initialization using defaults
dist1.setInches(6.7);
Distance dist2(1,7); // showing initialization to a distance
Distance dist3(0,34); // showing the auto-conversion of inches to feet as needed
Distance dist4(dist3); // showing initialization to another distance object's values
dist4.setFeet(4);
Distance dist_array[4] = {dist1, dist2, dist3, dist4};
int menu_entry;
bool menu_exit = false;
do
{
//display menu
cout << endl << endl << endl;
cout << "***** Main Menu *****" << endl;
cout << "1: Add two distances" << endl;
cout << "2: Subtract two distances" << endl;
cout << "3: Sum an array of distances" << endl;
cout << "4: Plot an array of distances" << endl;
cout << "5: Calculate the perimeter and area of a rectangle" << endl;
cout << "6: Exit" << endl;
cout << "Please enter the number of your selection: ";
cin >> menu_entry;
cin.ignore(256,'\n'); // clear input buffer of remaining data
// check entry, warn and promt again if invalid
if ((menu_entry < 1) || (menu_entry > 6))
{
cout << endl;
cout << "Invalid entry! Please try again." << endl;
continue;
}
//choose and run function based on user menu selection
switch (menu_entry)
{
case 1:
addDistances(dist2, dist3);
break;
case 2:
subtractDistances(dist2, dist3);
break;
case 3:
sumDistanceArray(dist_array);
break;
case 4:
plotDistanceArray(dist_array);
break;
case 5:
calcRectangle(dist2, dist3);
break;
case 6:
default:
menu_exit = true; //if we picked 6 then exit
break;
}
}
while(!menu_exit);
return 0;
}
void addDistances(Distance dist1, Distance dist2)
{
Distance dist_total;
dist_total = dist1 + dist2;
cout << endl;
cout << "The sum of " << dist1.showDistance() << " and " << dist2.showDistance() << " is "
<< dist_total.showDistance() << endl;
}
void subtractDistances(Distance dist1, Distance dist2)
{
Distance dist_total;
dist_total = dist1 - dist2;
cout << endl;
cout << "The difference of " << dist1.showDistance() << " and " << dist2.showDistance() << " is "
<< dist_total.showDistance() << endl;
}
void sumDistanceArray(Distance dist_array[])
{
int i;
int num_distances = 4;
Distance dist_total;
for(i=0; i<num_distances; i++)
{
dist_total = dist_total + dist_array[i];
}
cout << endl;
cout << "The sum of: " << endl;
for(i=0; i<num_distances; i++)
{
cout << dist_array[i].showDistance() << endl;
}
cout << "is " << dist_total.showDistance() << endl;
}
void plotDistanceArray(Distance dist_array[])
{
int i;
int j;
double inches;
double bar_percentage; // percentage of total columns bar takes up
int bar_length; // number of columns bar is long
int num_distances = 4;
int num_feet_max = 6; // the max of the graph scale
int num_max_disp_columns = 60; // the number of max columns a bar can take up
cout << endl;
cout << "Plot of an array of distances (6 ft scale): " << endl;
for(i=0; i<num_distances; i++)
{
cout << setw(20) << dist_array[i].showDistance() << " ";
inches = dist_array[i].getInches() + (12 * dist_array[i].getFeet());
bar_percentage = inches / (num_feet_max*12);
bar_length = bar_percentage * num_max_disp_columns; // calculate bar length
for(j=0; j<(bar_length+1); j++)
{
cout << "#"; // draw bar
}
cout << endl;
}
}
void calcRectangle(Distance dist1, Distance dist2)
{
Distance rect_area;
Distance rect_perimeter;
double inches1;
double inches2;
inches1 = dist1.getInches() + (12 * dist1.getFeet());
inches2 = dist2.getInches() + (12 * dist2.getFeet());
rect_area.setInches(inches1 * inches2);
rect_perimeter.setInches((2 * inches1) + (2 * inches2));
cout << endl;
cout << "A rectangle with dimensions " << dist1.showDistance() << " and " << dist2.showDistance() << " has an area of "
<< rect_area.showDistance() << " and a perimeter of " << rect_perimeter.showDistance() << endl;
}
我按照你告诉我的方式改变了它,但仍然是同样的问题。在尝试我改变所有距离后(例如对于操作员而言)我将它们改为常数,仍然是同样的问题。
答案 0 :(得分:0)
尝试将Distance(Distance&amp;)的签名更改为Distance(const Distance&amp;)。关键是临时值dist1 + dist2不会与“距离&amp;”匹配,但它将与“const Distance&amp;”相匹配。