Solved

Why doesn't following program compile ?

Posted on 2013-10-31
6
244 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
 
LVL 1

Author Comment

by:naseeam
ID: 39615237
= =   doesn't work for comparing variables that are type class ?  It must be overloaded ?
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

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…
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 use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

770 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