0

I'm taking a programming 101 class and I have an assignment to do which has me modify the following example files. I'm not asking for help on that part as I think I understand what I have to do to the files. My issue is that the files as given (both typed by hand from the text and copy/pasted directly from the given example files) result in the same error.

As far as I can tell, the example is to setup the header file to be reusable (as headers are). And then to make a call to the header to obtain the course name and insert it into the welcome message.

I'm using Code::Blocks 16.01. I selected the GNU GCC Compiler (C++11) as recommended by the book (a Pearson product). I've set my project up as a console application, and all the other code I have run has been the same type of application and has compiled/worked without error. I have done two examples using the split source code and header file. The last one worked perfectly. So I'm not sure what is wrong.

Build Log

-------------- Build: Debug in PE3-11 (compiler: GNU GCC Compiler)---------------
mingw32-g++.exe  -o bin\Debug\PE3-11.exe obj\Debug\GradeBook.o   
C:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/4.9.2/../../../libmingw32.a(main.o):main.c:(.text.startup+0xa7): undefined reference to `WinMain@16'
collect2.exe: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 0 second(s))
2 error(s), 0 warning(s) (0 minute(s), 0 second(s))

So two questions:

What is causing the error? Aren't I supposed to have an int main() somewhere?

GradeBook.h

// Fig. 3.11: GradeBook.h
// GradeBook class definition. This file presents GradeBook's public
// interface without revealing the implementations of GradeBook's member
// functions, which are defined in GradeBook.cpp.
#include <string> // class GradeBook uses C++ standard string class
// GradeBook class definition
class GradeBook
{
public:
   explicit GradeBook( std::string ); // constructor initialize courseName
   void setCourseName( std::string ); // sets the course name
   std::string getCourseName() const; // gets the course name
   void displayMessage() const; // displays a welcome message
private:
   std::string courseName; // course name for this GradeBook
}; // end class GradeBook

GradeBook.cpp

// Fig. 3.12: GradeBook.cpp
// GradeBook member-function definitions. This file contains
// implementations of the member functions prototyped in GradeBook.h.
#include <iostream>
#include "GradeBook.h" // include definition of class GradeBook
using namespace std;

// constructor initializes courseName with string supplied as argument
GradeBook::GradeBook( string name )
   : courseName( name ) // member initializer to initialize courseName
{
   // empty body
} // end GradeBook constructor

// function to set the course name
void GradeBook::setCourseName( string name )
{
   courseName = name; // store the course name in the object
} // end function setCourseName

// function to get the course name
string GradeBook::getCourseName() const
{
   return courseName; // return object's courseName
} // end function getCourseName

// display a welcome message to the GradeBook user
void GradeBook::displayMessage() const
{
   // call getCourseName to get the courseName
   cout << "Welcome to the grade book for\n" << getCourseName()
      << "!" << endl;
} // end function displayMessage

There is a third file, which according to the book is part of a separate exercise, but seems to be related, listed below. The issue there is, aside from a common use of the header, I don't see how these .cpp's link. But it also includes the int main() as I expected. So I'm really confused.

This file also refs the same header (and works fine with just the header in the build/project), but it creates two grade book objects and then tells us it's done so. So I'm not sure what is missing from the previous .cpp to make it work, or how/why it integrates with this one.

// Fig. 3.13: fig03_13.cpp
// GradeBook class demonstration after separating
// its interface from its implementation.
#include <iostream>
#include "GradeBook.h" // include definition of class GradeBook
using namespace std;

// function main begins program execution
int main()
{
   // create two GradeBook objects
   GradeBook gradeBook1( "CS101 Introduction to C++ Programming" );
   GradeBook gradeBook2( "CS102 Data Structures in C++" );

   // display initial value of courseName for each GradeBook
   cout << "gradeBook1 created for course: " << gradeBook1.getCourseName()
      << "\ngradeBook2 created for course: " << gradeBook2.getCourseName()
      << endl;
} // end main
C-Mac
  • 31
  • 5
  • Sounds like you set up a "Windows API" project, due to `WinMain` not being defined, and not a generic console project. – PaulMcKenzie Jun 13 '16 at 04:55
  • Yeah, I have gathered as much from other people posting similar. The problem is I have selected *console application* or *empty project* each time. I would literally have to scroll down to the bottom of the template wizard to select the Windows GUI/API project. – C-Mac Jun 13 '16 at 05:04
  • And i just double checked, the *Properties: Bluid Targets* tab has a *type* drop down. *Console Application* is selected. – C-Mac Jun 13 '16 at 05:08
  • I went thru that page that I "duplicated" five times and I haven't been able to tease out the solution. So I don't see answer to my question on that page... – C-Mac Jun 16 '16 at 15:46
  • Hey, I figured it out. **And it wasn't listed on the page I "duplicated".** Turns out with Code::Blocks, you have to tell it to link all the files in the directory. Right click your Project, select Properties, select the Build Targets tab, and ensure all the files in your project have a check mark. If any files don't show up, hit chancel, save your project, and repeat the process. – C-Mac Jun 20 '16 at 00:22

0 Answers0