我的头文件:
// Definition of class SunshineWeb that counts Product 1, Product 2
// and Product 3 retail prices. Refer to Project 2.cpp for member
// functions.
using namespace std;
#include <string> // program uses C++ standard string class
// SunshineWeb class definition
class SunshineWeb
{
public:
SunshineWeb( string ); // constructor initialises customer name
void setCustomerName( string ); // function to get customer name
string getCustomerName; // function to retrieve customer name
void displayMessage(); // displays a welcome message
void inputProducts(); // inputs a number of products
void displayProductTotal(); // total number of what products
private:
string CustomerName; // customer's name
int Product1Count;
int Product2Count;
int Product3Count;
}; // end of class SunshineWeb
成员函数定义、开关计数器和所有这些:
// Project 2.cpp : Defines the entry point for the console application.
// Member-function definitions for class SunshineWeb that uses a switch
// statement to count Products, then calculate and display Product total.
#include "stdafx.h"
#include <iostream>
#include "SunshineWeb.h"
#include <iomanip>
using namespace std;
//constructor initialises CustomerName with string supplied as arguement;
//initialises counter data member to 0
SunshineWeb::SunshineWeb( string name )
{
setCustomerName( name ); // validates and store CustomerName
Product1Count = 0; // initialises count of Product 1 to 0
Product2Count = 0; // initialises count of Product 2 to 0
Product3Count = 0; // initialises count of Product 3 to 0
} // end SunshineWeb constructor
// function to set the customer's name; limits to 25 or fewer characters
void SunshineWeb::setCustomerName( string name )
{
if ( name.length() <= 25 ) // if the name has 25 or fewer characters
CustomerName = name;
else // if name is longer than 25 characters
{ // sets CustomerName to the first 25 characters of parameter name
CustomerName = name.substr( 0, 25 ); // selects first 25 characters
cout << "Name \"" << name << "\" exceeds maximum length (25). \n"
<< "Limiting CustomerName to first 25 characters.\n" << endl;
} // end if... else statement
} // end function setCustomerName
// function to retrieve customer's name
string SunshineWeb::getCustomerName()
{
return CustomerName;
}
// displays a welcome message to the user
void SunshineWeb::displayMessage()
{
cout << "Welcome to Sunshine Web's store application!" << endl;
} // end function displayMessage
void SunshineWeb::inputProducts()
{
int products; // products entered by user
cout << "Enter 1 for Product 1, 2 for Product 2, " << endl
<< "3 for Product 3, and EOF character to end input." << endl;
// loops until user inputs end-of-file key sequence
while( ( products = cin.get() ) != EOF )
{
// determine which product was entered
switch (products) // switch statement nested in while
{
case '1': // The character 1 was inputted
++Product1Count; // increment Product1Count
break; // necessary to exit the switch
case '2': // The character 2 was inputted
++Product2Count; // increment Product2Count
break; // necessary to exit the switch
case '3': // The character 3 was inputted
++Product3Count; // increment Product3Count
break; // necessary to exit the switch
case '\n': // ignores new lines,
case '\t': // tabs,
case ' ' : // and spaces inbetween input
break; // exit switch
default: // catch all other characters
cout << "Incorrect character entered."
<< "Please enter 1, 2, 3 or EOF key." << endl;
break; // exit switch
} // end switch
} // end while
} // end function inputProducts
// displays the quantity of the product and retail total
void SunshineWeb::displayProductTotal()
{
// output summary of user orders
cout << "Quantity of each Product ordererd by the user:"
<< "\nProduct 1, at $22.98 per unit: " << Product1Count // displays number of Product 1
<< "\nProduct 2, at $34.50 per unit: " << Product2Count // displays number of Product 2
<< "\nProduct 3, at $99.98 per unit: " << Product3Count // displays number of Product 3
<< endl;
// algorithim used to calculate total price
int total = static_cast <double> (Product1Count*22.98) + static_cast <double>(Product2Count*34.50) + static_cast<double>(Product3Count*99.98);
cout << "The total price of your order is: " << fixed << setprecision(2) << total << endl;
} // end function displayProductTotal
问题从 开始string SunshineWeb::getCustomerName()
,它强调getCustomerName()
并说明以下内容:
Error:declaration is incompatible with "std::string SunshineWeb::getCustomerName"
(declared at line 12 of "c:\users\user\documents\visual studio 2010\projects\project 2\project 2\SunshineWeb.h")
这是主文件/执行程序的东西:
// creates SunshineWeb object, inputs products and displays
// total quantity and retail price of products inputted.
#include "SunshineWeb.h" // includes definition of class SunshineWeb
int main()
{
// creates SunshineWeb object
SunshineWeb mySunshineWeb("Sunshine Web Store");
mySunshineWeb.displayMessage();
mySunshineWeb.inputProducts();
mySunshineWeb.displayProductTotal();
} // end main
我的第二个问题是SunshineWeb mySunshineWeb("Sunshine Web Store");
,它强调Sunshine Web Store
并指出:
Error: no suitable constructor exists to convert from "const char[19]" to "SunshineWeb"
刚开始学习 C++ 面向对象编程,这是我为课堂编写的程序 - 有什么用?我用谷歌搜索并浏览了我的笔记,但它根本没有帮助/解决这个问题。真正让我烦恼的是没有合适的构造函数部分,因为我可以简单地将所有与客户名称相关的函数扔出窗口,因为我什至还没有实现它,因为我无法让它工作;但也许我缺少一些东西。不管怎样……我需要帮助!请救救我的宝贝!
编辑:感谢您的帮助,我注意到我的错误并尝试按照建议进行调整,所以我的头文件现在看起来像:
// Definition of class SunshineWeb that counts Product 1, Product 2
// and Product 3 retail prices. Refer to Project 2.cpp for member
// functions.
#include <string> // program uses C++ standard string class
// SunshineWeb class definition
class SunshineWeb
{
public:
SunshineWeb( std::string ); // constructor initialises customer name
void setCustomerName( std::string ); // function to get customer name
std::string getCustomerName(); // function to retrieve customer name
void displayMessage(); // displays a welcome message
void inputProducts(); // inputs a number of products
void displayProductTotal(); // total number of what products
private:
std::string CustomerName; // customer's name
int Product1Count;
int Product2Count;
int Product3Count;
}; // end of class SunshineWeb
这修复了之前的不兼容声明错误(一个简单的拼写错误),我不确定它是否在没有合适的构造函数的情况下修复了错误,因为虽然讨厌的红色下划线消失了,但我仍然无法运行程序:我打开 Visual Studio 2010 的错误列表,我看到几个错误:
Error 5 error C2065: 'mySunshineWeb' : undeclared identifier c:\users\user\documents\visual studio 2010\projects\project 2\project 2\sunshineweb.cpp 9
Error 7 error C2065: 'mySunshineWeb' : undeclared identifier c:\users\user\documents\visual studio 2010\projects\project 2\project 2\sunshineweb.cpp 10
Error 9 error C2065: 'mySunshineWeb' : undeclared identifier c:\users\user\documents\visual studio 2010\projects\project 2\project 2\sunshineweb.cpp 11
Error 2 error C2065: 'SunshineWeb' : undeclared identifier c:\users\user\documents\visual studio 2010\projects\project 2\project 2\sunshineweb.cpp 8
Error 3 error C2146: syntax error : missing ';' before identifier 'mySunshineWeb' c:\users\user\documents\visual studio 2010\projects\project 2\project 2\sunshineweb.cpp 8
Error 6 error C2228: left of '.displayMessage' must have class/struct/union c:\users\user\documents\visual studio 2010\projects\project 2\project 2\sunshineweb.cpp 9
Error 10 error C2228: left of '.displayProductTotal' must have class/struct/union c:\users\user\documents\visual studio 2010\projects\project 2\project 2\sunshineweb.cpp 11
Error 8 error C2228: left of '.inputProducts' must have class/struct/union c:\users\user\documents\visual studio 2010\projects\project 2\project 2\sunshineweb.cpp 10
Error 4 error C3861: 'mySunshineWeb': identifier not found c:\users\user\documents\visual studio 2010\projects\project 2\project 2\sunshineweb.cpp 8
Warning 1 warning C4627: '#include "SunshineWeb.h"': skipped when looking for precompiled header use c:\users\user\documents\visual studio 2010\projects\project 2\project 2\sunshineweb.cpp 3
是的,我是一个完全的业余爱好者,犯了(大部分)愚蠢的错误,但为了我生命中的挚爱,我似乎无法找到解决我看似简单的问题的方法。这些问题毫无意义,我正在检查一切。感谢您的帮助(和耐心显示),我真的很感激。