?
Solved

Why doesn't following program compile ?

Posted on 2013-10-31
6
Medium Priority
?
261 Views
Last Modified: 2013-10-31
#include <iostream>
#include <string>


class GradeBook
{
public:
   GradeBook( string ); 
   void setCourseName( string ); 
   string getCourseName(); 
   void displayMessage();    
   
private:
   string courseName; 
};


int main()
{

  GradeBook gradebook1, another;
  
  GradeBook * ptr = &gradebook1;
  another = *ptr;

  if (gradebook1 == another)
  {
     std::cout << "They are equal\n";
  }
  return 0;
}

Open in new window


The first error is as follows:  
test.cpp:8:22:  error:  field 'string' has incomplete type
        GradeBook( string );
0
Comment
Question by:naseeam
  • 3
  • 2
6 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 1400 total points
ID: 39615213
Three issues with that code:

-  you forgot to use 'using namespace std:'
- you are missing a default constructor for 'GradeBook'
- the class is also missing a comarison operator

The following works, see the lines marked witn '// <---':

#include <iostream>
#include <string>
using namespace std; // <---

class GradeBook
{
public:
   GradeBook( string ); 
   GradeBook() {}; // <---
   void setCourseName( string ); 
   string getCourseName(); 
   void displayMessage(); 

   bool operator==(const GradeBook& r) const { return courseName == r.courseName;} // <---
   
private:
   string courseName; 
};


int main()
{

  GradeBook gradebook1, another;
  
  GradeBook * ptr = &gradebook1;
  another = *ptr;

  if (gradebook1 == another)
  {
     std::cout << "They are equal\n";
  }
  return 0;
}
                                  

Open in new window

0
 
LVL 32

Assisted Solution

by:phoffric
phoffric earned 200 total points
ID: 39615214
See if std::string helps.
0
 
LVL 1

Author Comment

by:naseeam
ID: 39615237
= =   doesn't work for comparing variables that are type class ?  It must be overloaded ?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 1

Author Comment

by:naseeam
ID: 39615247
std::string works
0
 
LVL 86

Expert Comment

by:jkr
ID: 39615263
>>= =   doesn't work for comparing variables that are type class ?  It must be overloaded ?

Yes, there is no way to do that for complex types without an overload. Compilers can't be that smart ;o)

Oh, and prefixing 'string' with 'std' is not necessary if you refer to the namespace as above.
0
 
LVL 1

Author Closing Comment

by:naseeam
ID: 39615356
Thank you!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question