Fairly new to C++, but familiar with programming to a decent degree. According to the instructions, I've separated the declarations and definitions of my methods into a header file and source file respectively. I'm trying to have another source file access the methods. I've included the header in both of the source files, so they should have access to each other if I'm understanding how it works correctly. However, running the program gives me several unresolved external symbol errors. The code is as follows.
#ifndef SELLER_H
#define SELLER_H
//*****************************************************************
// FILE: Seller.h
// AUTHOR: your name
// LOGON ID: your z-ID
// DUE DATE: due date of assignment
//
// PURPOSE: Contains the declaration for the Seller class.
//*****************************************************************
class Seller
{
// Data members and method prototypes for the Seller class go here
public:
staticchar name[31];
staticdouble total;
Seller::Seller();
Seller::Seller(char nameArray[], double salesTotal);
char* getName();
double getSalesTotal();
void setSalesTotal(double newTotal);
void print();
};
#endif
I have a feeling that I'm making a really simple error somewhere, but I haven't been able to find it in a few hours. I would really appreciate a fresh set of eyes on this.
#ifndef SELLER_H
#define SELLER_H
//*****************************************************************
// FILE: Seller.h
// AUTHOR: your name
// LOGON ID: your z-ID
// DUE DATE: due date of assignment
//
// PURPOSE: Contains the declaration for the Seller class.
//*****************************************************************
class Seller
{
// Data members and method prototypes for the Seller class go here
public:
std::string name;
double total;
Seller();
Seller(std::string sellerName, double salesTotal);
std::string getName();
double getSalesTotal();
void setSalesTotal(double newTotal);
void print();
};
#endif
#include <iostream>
#include <string>
#include "Seller.h"
using std::cout;
using std::endl;
int main()
{
// Test default constructor
Seller seller1;
// Text alternate constructor
std::string s = "Jones, Martin";
Seller seller2(s, 1234.56);
// Test print() method and whether constructors
// properly initialized objects
cout << "Printing seller1\n";
seller1.print();
cout << endl;
cout << "Printing seller2\n";
seller2.print();
cout << endl << endl;
// Test accessor methods
cout << seller2.getName() << endl;
cout << seller2.getSalesTotal() << endl;
seller2.setSalesTotal(6543.21);
cout << seller2.getSalesTotal() << endl;
return 0;
}
I used a C++ string because using a char array crashed the program after initializing the two objects. And I was too lazy to try to fix the problem to work with a char array.
Here's the output of the revised program:
Printing seller1
Generic Seller 0.00
Printing seller2
Jones, Martin 1234.56
Jones, Martin
1234.56
6543.21
BTW, I figured out why the char array crashed the program. It was your 2nd constructor, when you tried to copy the passed char array to the member char array.
You transposed the strcpy() parameters.
I still prefer C++ strings in C++ code, easier to deal with.