[RESOLVED] I"m a NOOB...Why isn't my code working!?
2 days ago I got the book C++ How to Program /8e by Paul & Harvey Deitel.
In the course of working my way through the book I got stuck on chapter 3
Section 7: Separating Interface from Implementation gradebook case study
Anyway here's what happened:
This is my gradebook1.cpp
// gb.cpp GradeBook class demonstration after seperating
// its interface from its implementation
#include "stdafx.h"
#include <iostream>
#include <string>
#include "gb1.h"
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
This is my gb1.cpp
// gb1.cpp GradeBook member-function definitions. This file contains
// implementations of the member functions prototyped in gb.h.
#include "stdafx.h"
#include <iostream>
#include <string>
#include "gb1.h"
using namespace std;
// constructor initializes courseName with string supplied as argument
GradeBook::GradeBook( string name )
{
setCourseName( name ); // call set function to initialize courseName
} // 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()
{
return courseName; // return object's courseName
} // end function getCourseName
// display a welcome message to the GradeBook user
void GradeBook::displayMessage()
{
// call getCourseName to get the courseName
cout << "Welcome to the grade book for\n" << getCourseName()
<< "!" << endl;
} // end function displayMessage
This is my gb1.h
// gb1.h Gradebook class definition. This file presents GradeBook's public
// interface without revealing the implementation of GradeBook's member
// functions, which are defined in gb1.cpp.
#pragma once
#include "stdafx.h"
#include <iostream>
#include <string> // class GradeBook uses C++ standard string class
using namespace std;
// GradeBook class definition
class GradeBook
{
public:
GradeBook( string ); // constructor that initializes courseName
void setCourseName( string ); // function that sets the course name
string getCourseName(); // function that gets the course name
void displayMessage(); // function that displays a welcome message
private:
string courseName; // course name for this GradeBook
}; // end class GradeBook
This is the error message
1>gradebook1.obj : error LNK2019: unresolved external symbol "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall GradeBook::getCourseName(void)" (?getCourseName@GradeBook@@QAE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) referenced in function _main
1>gradebook1.obj : error LNK2019: unresolved external symbol "public: __thiscall GradeBook::GradeBook(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (??0GradeBook@@QAE@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function _main
Please help me fix whatever may be wrong, because I can't keep
going through the book while I'm stuck so early on.
Re: [RESOLVED] I"m a NOOB...Why isn't my code working!?
I figured out the problem wasn't the code it was the fact that it wasn't linked properly.
When I added gb1.h and gb1.cpp to their respective folders in the Visual 2010 IDE it did
not work because, even though they showed up on solution explorer, the files had to
physically be in the same as main gradebook1.cpp folder. After I put gb1.h and gb1.cpp
in the gradebook1.cpp solution folder it worked like a charm.
Re: [RESOLVED] I"m a NOOB...Why isn't my code working!?
Originally Posted by j_benjamin
I figured out the problem wasn't the code it was the fact that it wasn't linked properly.
When I added gb1.h and gb1.cpp to their respective folders in the Visual 2010 IDE it did
not work because, even though they showed up on solution explorer, the files had to
physically be in the same as main gradebook1.cpp folder. After I put gb1.h and gb1.cpp
in the gradebook1.cpp solution folder it worked like a charm.
It doesn't matter where on your computer (or a network share) the files are located, but the cpp files need to be included in the project to be compiled and linked. Unless you link against precompiled object files or libraries, but that's not something you'd normally do with your own code.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
Bookmarks