CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2012
    Posts
    2

    [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


    1. // gb.cpp GradeBook class demonstration after seperating
    2. // its interface from its implementation
    3. #include "stdafx.h"
    4. #include <iostream>
    5. #include <string>
    6. #include "gb1.h"
    7. using namespace std;
    8. // function main begins program execution
    9. int main()
    10. {
    11. // create two GradeBook objects
    12. GradeBook gradeBook1( "CS101 Introduction to C++ Programming" );
    13. GradeBook gradeBook2( "CS102 Data Structures in C++" );
    14. //display initial value of courseName for each GradeBook
    15. cout << "gradeBook1 created for course: " << gradeBook1.getCourseName()
    16. << "\ngradeBook2 created for course: " << gradeBook2.getCourseName()
    17. << endl;
    18. } // end main



    This is my gb1.cpp


    1. // gb1.cpp GradeBook member-function definitions. This file contains
    2. // implementations of the member functions prototyped in gb.h.
    3. #include "stdafx.h"
    4. #include <iostream>
    5. #include <string>
    6. #include "gb1.h"
    7. using namespace std;
    8. // constructor initializes courseName with string supplied as argument
    9. GradeBook::GradeBook( string name )
    10. {
    11. setCourseName( name ); // call set function to initialize courseName
    12. } // end GradeBook constructor
    13. // function to set the course name
    14. void GradeBook::setCourseName( string name )
    15. {
    16. courseName = name; // store the course name in the object
    17. } // end function setCourseName
    18. // function to get the course name
    19. string GradeBook::getCourseName()
    20. {
    21. return courseName; // return object's courseName
    22. } // end function getCourseName
    23. // display a welcome message to the GradeBook user
    24. void GradeBook:isplayMessage()
    25. {
    26. // call getCourseName to get the courseName
    27. cout << "Welcome to the grade book for\n" << getCourseName()
    28. << "!" << endl;
    29. } // end function displayMessage



    This is my gb1.h


    1. // gb1.h Gradebook class definition. This file presents GradeBook's public
    2. // interface without revealing the implementation of GradeBook's member
    3. // functions, which are defined in gb1.cpp.
    4. #pragma once
    5. #include "stdafx.h"
    6. #include <iostream>
    7. #include <string> // class GradeBook uses C++ standard string class
    8. using namespace std;
    9. // GradeBook class definition
    10. class GradeBook
    11. {
    12. public:
    13. GradeBook( string ); // constructor that initializes courseName
    14. void setCourseName( string ); // function that sets the course name
    15. string getCourseName(); // function that gets the course name
    16. void displayMessage(); // function that displays a welcome message
    17. private:
    18. string courseName; // course name for this GradeBook
    19. }; // 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.

  2. #2
    Join Date
    Jul 2009
    Location
    India
    Posts
    835

    Re: I"m a NOOB...Why isn't my code working!?

    I"m a NOOB...Why isn't my code working!
    You answered your own why your code is not working.

    Before start posting you should go through forum rules once, use Code tags in your post first.
    Last edited by hypheni; December 17th, 2012 at 12:57 AM.
    ◄◄ hypheni ►►

  3. #3
    Join Date
    Dec 2012
    Posts
    2

    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.

  4. #4
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: [RESOLVED] I"m a NOOB...Why isn't my code working!?

    Quote Originally Posted by j_benjamin View Post
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured