我从一本书中自学了C ++。我陷入了四步中的第二步。这第二部分,虽然我告诉我必须"使这个类通用,以便形状的边可以是整数或浮动。"
到目前为止,它是一个非常简单的程序。它有三个脚本。有main.cpp,shape.cpp和shape.h。
程序采用字符串中的对象名称,长度为浮点数,重要的是侧面。我有一个构造函数,它将对象,边和长度放在一起以创建一个通用的形状对象,我将在稍后进行升级。
但现在是让它更通用,所以我认为这与模板命令有关。它可能非常简单,我很抱歉浪费你的时间。似乎我必须改变线形状形状(object,objectSides,objectLength);但我不确定以哪种方式,因为其他示例似乎表明你在标题和其他类中这样做。
main.cpp | 35 |错误:在' shape' |之前缺少模板参数 main.cpp | 38 |错误:' shape'未在此范围中声明|
的main.cpp
#include <iostream>
#include "shape.h"
using namespace std;
int main() {
//Setting variables to 0;
string object;
int objectSides = 0;
int objectSidesTemp = 0;
float objectLength = 0;
float objectLengthTemp = 0;
//Taking input for user on object being made.
cout << "Object to make: ";
cin >> object;
//Input for sides being inputted. If below 3, asks user again for a valid number.
//Assigns in temp number until within bounds, then assigns to objectSides.
while (objectSidesTemp < 3){
cout << "How many sides (Must be 3 or greater): ";
cin >> objectSidesTemp;
}
objectSides = objectSidesTemp;
//Input for length being inputted. If 0 or below, asks user again for a valid number.
//Assigns in temp number until within bounds, then assigns to objectLength.
while (objectLengthTemp <= 0){
cout << "Length of sides: ";
cin >> objectLengthTemp;
}
objectLength = objectLengthTemp;
//Creates object of Shape called shape. Inputs the object name, sides, and length.
Shape shape(object, objectSides, objectLength);
//Displays object specifics.
shape.display();
}
shape.h
#ifndef SHAPE_H
#define SHAPE_H
#include <string>
using namespace std;
template <class T>
class Shape {
public:
Shape(string, T, float);
void setObject(string); //Used to ensure constructor works
// virtual void setObject(string) = 0;
//Used setObject as virtual function since constructor uses it to
//Make the object using the setObject function.
string getObject();
T getSides();
bool setSides(T);
float getLength();
void setLength (float);
float getPerimeter(int, float);
float getArea (int, float);
void display();
private:
string object;
T sides;
float length;
};
#endif
shape.cpp
#include <iostream>
#include <string>
#include "shape.h"
using namespace std;
//Constructor of Shape. Inpunts shape, sides, and length.
Shape::Shape (string shapes, int sides, float length){
setObject(shapes);
setSides(sides);
setLength(length);
} //End of Shape constructor
//Method to request the amount of sides, returns as an int
//Because there are no lesser than full sides.
template <class T>
T Shape::getSides() {
return sides;
} //End of function getSides
//Method to set the amount of sides. Return method is a bool.
//If sideNumber is 0 or less in sides, returns a false.
//If sideNumber is greater than 1, returns true.
//Would recommend setting minimum of sides to 3 or greater,
//as anything less is either a shape, a dot, or nothing.
bool Shape::setSides(int sideNumber) {
if (sides > 0){
sides = sideNumber;
return true;
}
else {
return false;
}
} //End of function setSides
//Returns string tied to the shape's object.
//Future in 2D suggests triangle, rectangle, or square.
//Implimented currently as any string.
string Shape::getObject(){
return object;
} //End of function getObject
//Sets input of the string to the variable object.
void Shape::setObject(string obj){
object = obj;
} //End of function setObject
//Retrieves float of length, as length can be numbers with decimals.
float Shape::getLength(){
return length;
} //End of function getLength
//Sets length of the object. Uses void because it doesn't need to return anything.
void Shape::setLength(float objectLength){
length = objectLength;
} //End of function setLength
//Sets area usings 3 sides or 4 sides else returns a 0.
//Returning as float because objectLength is used as a float.
float Shape::getArea (int objectSides, float objectLength) {
if (objectSides == 3){
return (objectSides * objectLength) / 2;
}
if (objectSides == 4){
return (objectLength * objectLength);
}
// return objectSides * objectLength;
return 0;
} //End of function getArea
//Float return of that parimeter. Multipling sides by length to achieve.
float Shape::getPerimeter (int objectSides, float objectLength){
return objectSides * objectLength;
} //End of function getParimeter.
//Display function shows the Shape's varaibles.
//Uses getParimeter and getArea commands to generate further information of the shape.
void Shape::display() {
cout << "Object: " << object << endl;
cout << "Sides: " << sides << endl;
cout << "Length: " << length << endl;
cout << "Perimeter: " << getPerimeter(sides, length) << endl;
cout << "Area: " << getArea(sides, length) << endl;
} //End of function display.
答案 0 :(得分:1)
您将Shape
定义为类模板,只使用一个模板参数 T
:
template <class T>
class Shape { /* ... */ };
因此,您需要在实例化T
时明确指定Shape
:
Shape<int> shape(object, objectSides, objectLength);
// ^^^