Note that there are some explanatory texts on larger screens.

plurals
  1. PONo Suitable Constructor, and an incompatible declaration error
    text
    copied!<p>My header file:</p> <pre><code>// 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 &lt;string&gt; // 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 </code></pre> <p>Member-function definitions, switch counter and all that: </p> <pre><code>// 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 &lt;iostream&gt; #include "SunshineWeb.h" #include &lt;iomanip&gt; 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() &lt;= 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 &lt;&lt; "Name \"" &lt;&lt; name &lt;&lt; "\" exceeds maximum length (25). \n" &lt;&lt; "Limiting CustomerName to first 25 characters.\n" &lt;&lt; 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 &lt;&lt; "Welcome to Sunshine Web's store application!" &lt;&lt; endl; } // end function displayMessage void SunshineWeb::inputProducts() { int products; // products entered by user cout &lt;&lt; "Enter 1 for Product 1, 2 for Product 2, " &lt;&lt; endl &lt;&lt; "3 for Product 3, and EOF character to end input." &lt;&lt; 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 &lt;&lt; "Incorrect character entered." &lt;&lt; "Please enter 1, 2, 3 or EOF key." &lt;&lt; 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 &lt;&lt; "Quantity of each Product ordererd by the user:" &lt;&lt; "\nProduct 1, at $22.98 per unit: " &lt;&lt; Product1Count // displays number of Product 1 &lt;&lt; "\nProduct 2, at $34.50 per unit: " &lt;&lt; Product2Count // displays number of Product 2 &lt;&lt; "\nProduct 3, at $99.98 per unit: " &lt;&lt; Product3Count // displays number of Product 3 &lt;&lt; endl; // algorithim used to calculate total price int total = static_cast &lt;double&gt; (Product1Count*22.98) + static_cast &lt;double&gt;(Product2Count*34.50) + static_cast&lt;double&gt;(Product3Count*99.98); cout &lt;&lt; "The total price of your order is: " &lt;&lt; fixed &lt;&lt; setprecision(2) &lt;&lt; total &lt;&lt; endl; } // end function displayProductTotal </code></pre> <p>Problem begins at <code>string SunshineWeb::getCustomerName()</code>, where it underlines <code>getCustomerName()</code> and states the following:</p> <pre><code>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") </code></pre> <p>And this is the main file / executor thingy:</p> <pre><code>// 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 </code></pre> <p>My second problem is with <code>SunshineWeb mySunshineWeb("Sunshine Web Store");</code>, where it underlines <code>Sunshine Web Store</code> and states:</p> <pre><code> Error: no suitable constructor exists to convert from "const char[19]" to "SunshineWeb" </code></pre> <p>Just beginning to learn C++ object orientation programming, and this is a program I wrote for class - what gives? I googled and went through my notes, but it doesn't help / solve this problem at all. What really bugs me most is the no suitable constructor part, as I can simply throw all Customer Name related functions out of the window, as I haven't even implemented it yet, seeing as I can't get it working ; but maybe there's something I'm missing. Either way... I need help! Please save my baby!</p> <p><strong>Edit</strong> : Thanks for the help, I've noticed my error and tried tweaking it like suggested, so my header file now looks like :</p> <pre><code>// Definition of class SunshineWeb that counts Product 1, Product 2 // and Product 3 retail prices. Refer to Project 2.cpp for member // functions. #include &lt;string&gt; // 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 </code></pre> <p>This fixed the incompatible declaration error (a simple typo mistake) from earlier, and I'm not sure if it fixed the error with no suitable constructor, because while the nasty red underline is gone, I still can't run the program : I opened up the error list from Visual Studio 2010, I see a couple of errors:</p> <pre><code>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 </code></pre> <p>Yes, I am a complete amateur making (mostly) silly mistakes, but for the love of my life I can't seem to find a solution to my seemingly-simple problems. These problems makes no sense whatsoever, and I'm quadruple checking everything. Thanks for the help (and patience displayed), I really appreciate it.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload