Solved

Why doesn't following program compile ?

Posted on 2013-10-31
6
241 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

707 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

17 Experts available now in Live!

Get 1:1 Help Now