我虽然现在一切正常但我最好发布修复过的代码:
#include <iostream>
#include <cmath>
using namespace std;
//Read the README file for all the details. Fill in the gaps below.
//Write your Spot class here
class Spot{
private:
int x, y;
public:
Spot(){
x = y = 0;
}
int bound(int z){
if(z>=99)
return 99;
if(z<=0)
return 0;
else return z;
}
Spot(int new_x, int new_y ){
x = bound(new_x);
y = bound(new_y);
}
int get_x(){
return x;
}
int get_y(){
return y;
}
void set_x(int new_x){
x = bound(new_x);
}
void set_y(int new_y){
y = bound(new_y);
}
int distance(){
return sqrt (x*x+y*y);
}
}
;
class Treasure{
private:
int x_min, x_max, y_min, y_max, treas_val;
public:
Treasure(){
x_min = 0;
x_max = 0;
y_min = 0;
y_max = 0;
treas_val = 0;
}
int bound(int x){
if(x>=99)
return 99;
if(x<=0)
return 0;
else return x;
}
Treasure(int max_x_new, int min_x_new, int max_y_new, int min_y_new, int treas_val_new){
x_max = bound(max_x_new);
x_min = bound(min_x_new);
y_max = bound(max_y_new);
y_min = bound(min_y_new);
//if (x_max < x_min)
// x_max = x_min;
//if (y_max < y_min)
// y_max = y_min;
//if (treas_val_new < 0)
// treas_val_new = 0;
treas_val = treas_val_new;
}
int get_x_min(){
return x_min;
}
int get_y_min(){
return y_min;
}
int get_x_max(){
return x_max;
}
int get_y_max(){
return y_max;
}
int get_value(){
return treas_val;
}
void set_x_min(int new_x_min){
x_min = bound(new_x_min);
}
void set_y_min(int new_y_min){
y_min = bound(new_y_min);
}
void set_x_max(int new_x_max){
x_max = bound(new_x_max);
}
void set_y_max(int new_y_max){
y_max = bound(new_y_max);
}
void set_value(int new_value){
if (new_value > 0)
treas_val = new_value;
else treas_val = 0;
}
bool within(Spot my_spot){
if ((my_spot.get_x() <= get_x_max()) || (my_spot.get_x() >= get_y_min()))
{
if ((my_spot.get_y() <= get_y_max())||(my_spot.get_y() >= get_x_min()))
return true;
}
else return false;
}
int loot(){
int tempval;
tempval = treas_val;
treas_val = 0;
return tempval;
}
};
#ifndef PROF_MAIN
//Nothing inside these #ifndef and #endif lines will be used in the autograder
//So feel free to use this main function to test your classes as you nclude <iostream>
//
//
//
//
int main() {
//Here's some sample code, which you can delete or use or whatever
cout << "Hello World!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1!\n";
Spot s(3,-4);
cout << s.get_x() << " " << s.get_y() << endl; //Should output "3 0"
s.set_x(50);
s.set_y(100);
cout << s.get_x() << " " << s.get_y() << endl; //Should output "50 99"
Treasure t(1,10,30,40,1000);
if (t.within(s)) {
cout << "Error, s is within t.\n";
}
Spot s2(5,35);
if (t.within(s2)) {
cout << "Correct, s2 is within t.\n";
}
cout << "Looting t: " << t.loot() << endl; //Should print 1000
cout << "Looting t: " << t.loot() << endl; //Should print 0
}
当我尝试编译代码时,我收到以下错误:
错误:在')'令牌之前预期的非限定ID class Spot(){
我无法找到问题所在。但是,我再也没有尝试过3年以上的程序,所以整个事情可能是一个丑陋的混乱。但是一步一步呃?
#include <iostream>
#include <cmath>
using namespace std;
class Spot() {
Private:
int x, y;
Public:
Spot() {
x = y = 0;
}
int bound(int x) {
if (x >= 99)
return 99;
if (x <= 0)
return 0;
else return x;
}
Spot(int new_x, int new_y ) {
new_x = bound(new_x);
new_y = bound(new_y);
x = new_x;
y = new_y;
}
int get_x() {
return x;
}
int get_y() {
return y;
}
void set_x(int new_x) {
new_x = bound(new_x);
x = new_x;
}
void set_y(int new_y) {
new_y = bound(new_y);
y = new_y;
}
int distance() {
return sqrt(x * x + y * y);
}
}
class Treasure() {
private:
int max_x, min_x, max_y, min_y, treas_val;
public:
Treasure() {
max_x = 0;
min_x = 0;
max_y = 0;
min_y = 0;
treas_val = 0;
}
int bound(int x) {
if (x >= 99)
return 99;
if (x <= 0)
return 0;
else return x;
Treasure(int max_x_new, int min_x_new, int max_y_new, int max_y_new, int treas_val_new) {
max_x = bound(max_x_new);
min_x = bound(min_x_new);
max_y = bound(max_y_new);
min_y = bound(min_y_new);
if (x_max < x_min)
x_max = x_min;
if (y_max < y_min)
y_max = y_min;
if (treas_val_new < 0)
treas_val_new = 0;
treas_val = treas_val_new;
}
int get_x_min() {
return x_min;
}
int get_y_min() {
return y_min;
}
int get_x_max() {
return x_max;
}
int get_y_max() {
return y_max;
}
int get_value() {
return treas_val;
}
void set_x_min(int new_x_min) {
x_min = bound(new_x_min);
}
void set_y_min(int new_y_min) {
y_min = bound(new_y_min);
}
void set_x_max(int new_x_max) {
x_max = bound(new_x_max);
}
void set_y_max(int new_y_max) {
y_max = bound(new_y_max);
}
void set_value(int new_value) {
if (new_value > 0)
treas_val = new_value;
else treas_val = 0;
}
bool within(spot my_spot) {
if ((my_spot::get_x <= get_x_max) || (my_spot::get_x >= get_y_min))
{
if ((my_spot::get_y <= get_y_max) || (my_spot::get_y >= get_x_min))
return true;
}
else return false;
}
int loot() {
int tempval;
tempval = treas_val;
treas_val = 0;
return tempval;
}
}
#ifndef PROF_MAIN
//Nothing inside these #ifndef and #endif lines will be used in the autograder
//So feel free to use this main function to test your classes as you nclude <iostream>
//
//
// write them
int main() {
//Here's some sample code, which you can delete or use or whatever
cout << "Hello World!!!!!1!\n";
Spot s(3, -4);
cout << s.get_x() << " " << s.get_y() << endl; //Should output "3 0"
s.set_x(50);
s.set_y(100);
cout << s.get_x() << " " << s.get_y() << endl; //Should output "50 99"
Treasure t(1, 10, 30, 40, 1000);
if (t.within(s)) {
cout << "Error, s is within t.\n";
}
Spot s2(5, 35);
if (t.within(s2)) {
cout << "Correct, s2 is within t.\n";
}
cout << "Looting t: " << t.loot() << endl; //Should print 1000
cout << "Looting t: " << t.loot() << endl; //Should print 0
}
#endif
答案 0 :(得分:6)
class Spot(){
应该是
class Spot{
(无括号)。同样适用于其他类定义。您有更多错误,例如Public:
而不是public:
等。请查看好的C ++入门手册并学习基本语法,例如: The Definitive C++ Book Guide and List