0
votes

I know this seems to be a common problem. However, I seem to be following all of the standard practices and guidelines for header files except for include guards which I don't think would hinder compilation. The linker errors described only occur when CustomerInformation.h is included into CPlusPlusTraining.cpp which contains the main method.

This is one source I used for C++ file organization information: http://www.umich.edu/~eecs381/handouts/CppHeaderFileGuidelines.pdf

Here is my header

class CustomerInformation {

public: CustomerInformation();

public: char InformationRequest();

};

Source

#include "stdafx.h"
#include <iostream>
#include <string>
#include "CustomerInformation.h"
using namespace std;

char InformationRequest() {
      string name;
      string lastName;
      int age;
      cout << "Please input your first and last name then your age: \n";
      cin >> name >> lastName >> age;
      cout << "Customer Information: " << name + " " << lastName + " " << age << "\n";

      char correction;
      cout << "Is all of this information correct? If it is Enter 'Y' if not Enter 'N' \n";
      cin >> correction;

      if (correction == 'N') {
        cout << "Please Enter your information again: \n";
        InformationRequest();
      }

      return correction;
}`

And my include into CPlusPlusTraining.cpp

#include "stdafx.h"
#include <iostream>
#include <string>
#include "CustomerInformation.h"
using namespace std;

Both the header file and source file have the same name. The header file ends in .h while the source file ends in .cpp. However I am getting numerous linker errors when compiling. What is the problem here? I'll save repetitive code inclusion for another day. Thanks.

How I call the method in my file with Main in it

CustomerInformation CI = CustomerInformation::CustomerInformation();
//information request
CI.InformationRequest(); //Not type safe for input

Error Details:

Build started: Project: CPlusPlusTraining, Configuration: Debug Win32 ------ 1> CPlusPlusTraining.cpp //File with Main 1>CPlusPlusTraining.obj : error LNK2019: unresolved external symbol "public: __thiscall CustomerInformation::CustomerInformation(void)" (??0CustomerInformation@@QAE@XZ) referenced in function _wmain 1>CPlusPlusTraining.obj : error LNK2019: unresolved external symbol "public: char __thiscall CustomerInformation::InformationRequest(void)" (?InformationRequest@CustomerInformation@@QAEDXZ) referenced in function _wmain 1>C:\Users\Gordlo_2\documents\visual studio 2013\Projects\CPlusPlusTraining\Debug\CPlusPlusTraining.exe : fatal error LNK1120: 2 unresolved externals ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

1
PS this is not homework, I am simply going through a book by Bjarne Stroustrup to get the basics of c++ down.gordlonious
What are the linker errors? Are you building with the cpp files ie: g++ main.cpp InfoRequest.cppFantastic Mr Fox
See my edit for details on the call and errors.gordlonious
There are some answers here: stackoverflow.com/questions/9928238/… It seems like you are not including the cpp in your linking stage.Fantastic Mr Fox

1 Answers

2
votes

You declared the member function of the Customer class without the class declaration, causing the compiler not to know it was your function you prototyped. It should be:

 char CustomerInformation::InformationRequest() {
 //your function stuff
 }