In my header file I have:

#include <iostream>
#include <string>
using namespace std;

class Customer
{
public: 

	//Output
	void displayLabel(ostream &out);

And in my main:

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include "Customer.h"

using namespace std;

Customer cust;
string blank = " ";
string line;
int start, end, count;
string titl, firstNam, middleNam, lastNam, add, cit, stat, zip, phone;

int main()
{
	cust.displayLabel();

NOTE: I took out a lot of unnecessary code to get straight to the point.

I know that my parameters do not match up, but that's my question: what do I send to the function parameter wise for ostream &out? I'm kind of lost in this part...

Thanks!

mitrmkar commented: ++rep for taking out unnecessary code. +5

Dani AI

Generated

Short answer: passing a stream (for example std::cout, an std::ofstream, or an std::ostringstream) to the method is the right approach — was pointing in the right direction. The linker error you showed, though, is not caused by cout; it means the program is trying to call a constructor or method that was declared but never defined (as suggested) or the translation unit that defines it isn't being linked.

Provide a concrete fix: implement the missing symbols in the .cpp that corresponds to your header and make sure that .cpp is compiled and linked. Example implementation sketch:

// Customer.cpp
#include "Customer.h"

Customer::Customer() {
    // initialize member data
}

void Customer::displayLabel(std::ostream &out) const {
    out << fullName << '\n' << address << '\n';
}

If you prefer not to deal with global/static initialization issues, create the object inside main and pass the stream explicitly. To write to a file, open an std::ofstream and pass it to the method:

std::ofstream out("label.txt");
if (out) {
    client.displayLabel(out);
}

Checklist of things to verify:

  • The .cpp that implements Customer is added to the project and actually built.
  • Declarations and definitions match exactly (including std:: qualification and const if used).
  • If you declared a non-default constructor, construct the object with appropriate arguments or add a default one.
  • Avoid using namespace std; in headers; prefer std::ostream in the class declaration.
    Following those steps will resolve the LNK2019 and let you pass streams (console or files) into displayLabel as intended.

Recommended Answers

All 3 Replies

If you want the data displayed on the console sceen then pass cout like this: cust.displayLabel(cout);

If you want the data displayed on the console sceen then pass cout like this: cust.displayLabel(cout);

I get an error when I put cout as a parameter.
Something like this:

1>CustomerClient.obj : error LNK2019: unresolved external symbol "public: __thiscall Customer::Customer(void)" (??0Customer@@QAE@XZ) referenced in function "void __cdecl `dynamic initializer for 'cust''(void)" (??__Ecust@@YAXXZ)
1>C:\Users\Joseph\Documents\Visual Studio 2008\Projects\Homework 7\Debug\Homework 7.exe : fatal error LNK1120: 1 unresolved externals

I get an error when I put cout as a parameter.

Using cout as a parameter is OK - that's not the error.

Looking at the error message, it seems that you don't have written code for default constructor of class Customer .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.