Solved

Why doesn't following program compile ?

Posted on 2013-10-31
6
242 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 350 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 50 total points
ID: 39615214
See if std::string helps.
0
 

Author Comment

by:naseeam
ID: 39615237
= =   doesn't work for comparing variables that are type class ?  It must be overloaded ?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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
 

Author Closing Comment

by:naseeam
ID: 39615356
Thank you!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

867 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now