0

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 ==========

lonious
  • 676
  • 9
  • 25

1 Answers1

2

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
 }
  • That solved one error! I still have two errors mentioning unresolved externals. – lonious Sep 09 '14 at 06:46
  • 1
    Not declared. Defined. The declaration is in the class body, and is correct, afaict. – Benjamin Lindley Sep 09 '14 at 06:47
  • 1
    @gordlonious: The other error is because you declared, but didn't define, the constructor. Either remove the declaration if you don't need to do anything special to initialise the class, or implement it if you do. – Mike Seymour Sep 09 '14 at 06:50