Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Why doesn't following program compile ?

Posted on 2013-10-31
6
Medium Priority
?
258 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

609 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