Member Avatar for Member #944671

I simply cannot figure this program out (nor why I am going in debt paying professors to "teach" me). Our assignment begins with reading a data from a file and storing it in an object array and then displaying the contents of that array.

When I compile the program in MS Visual Studio I get the following error:

error C2664: 'printArray' : cannot convert parameter 1 from 'Investment [1000]' to 'Investment'
1> No constructor could take the source type, or constructor overload resolution was ambiguous

I do not understand what is causing this error or how to fix it.

Any help would be appreciated.

Investment.h code

#include <string>

using namespace std;

class Investment
{
	private:
		string lastname;
		float amount;		
		float rate;			
		int month;
		int day;
		int year;

	public:
		Investment();
		Investment(string n, float a, float r, int m, int d, int y);

		void setName(string n); 
		void setAmount(float a);
		void setRate(float r);
		void setMonth(int m);
		void setDay(int d);
		void setYear(int y);

		string getName();
		float getAmount();
		float getRate();
		int getMonth();
		int getDay();
		int getYear();
		
		float getValue(int n);
		void printDate(ostream& outf);

};

Investment.cpp code

#include "Investment.h"
#include <string>
#include <iostream>
#include <cmath>

using namespace std;

Investment::Investment()
{
	lastname="unknown";
	amount=0.0;
	rate=0.0;
	month=1;
	day=1;
	year=1900;
}

Investment::Investment(string n, float a, float r, int m, int d, int y)
{
	lastname = n;
	amount = a;
	rate = r;
	month=m;
	day=d;
	year=y;
}

void Investment::setName(string n)
{
	lastname = n;
}

void Investment::setAmount(float a)
{
	amount = a;
}

void Investment::setRate(float r)
{
	rate = r;
}

void Investment::setMonth(int m)
{
	month = m;
}

void Investment::setDay(int d)
{
	day = d;
}

void Investment::setYear(int y)
{
	year = y;
}

string Investment::getName()
{
	return lastname;
}

float Investment::getAmount()
{
	return amount;
}

float Investment::getRate()
{
	return rate;
}

int Investment::getMonth()
{
	return month;
}

int Investment::getDay()
{
	return day;
}

int Investment::getYear()
{
	return year;
}

void Investment::printDate(ostream& outf)
{
	outf << month << "/" << day << "/" << year;
}

Project2.cpp

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <iomanip>
#include "Investment.h"

using namespace std;

#define N 1000

void printArray(Investment, int);

int main()
{
	Investment InvestmentList[N];
	string filename, n, FindName;
	int NoYears, FindAmt, m, d, y;
	int noinvestors=0;
	ifstream inputdata;
	float a, r;

	cout << "Input power data file name: ";
	cin >> filename;

	inputdata.open(filename.c_str());

	if(inputdata.fail())
	{
		cerr << "Error opening input file" << endl;
		system("pause");
		exit(1);
	}

	inputdata >> n >> a >> r >> m >> d >> y;
	int i=0;

	while ( !inputdata.eof() )
	{
		InvestmentList[i].setName(n);
		InvestmentList[i].setAmount(a);
		InvestmentList[i].setRate(r);
		InvestmentList[i].setMonth(m);
		InvestmentList[i].setDay(d);
		InvestmentList[i].setYear(y);
				
		inputdata >> n >> a >> r >> m >> d >> y;

		i = i + 1;
		noinvestors=noinvestors+1;
	}

	inputdata.close();

	cout << "number of investors: " << noinvestors << endl;
	system("pause");

	printArray(InvestmentList, noinvestors);
}

void printArray(Investment x[], int no)
{
cout << "NAME" << setw(25) << "AMOUNT" << setw(15) << "RATE" << 
		setw(15) << "DATE" <<endl;
	for (int i=0; i<no; i++)
	{
		cout << x[i].getName() << setw(13) << "\t" << 
				x[i].getAmount() << setw(5) << "\t" << 
				x[i].getRate() << setw(5) << "\t"; 
		x[i].printDate(cout);
		cout << endl;
	}
}

Dani AI

Generated

The compiler error comes from a parameter-type mismatch. The forward declaration in Project2.cpp declares printArray as taking a single Investment by value, but the call passes InvestmentList (an array). An array cannot be converted into one Investment object, so the call fails. was right: the function must accept an array or pointer (or a container) — and the declaration and the definition must match.

Minimal fixes (make declaration match the call)

void printArray(const Investment* list, int count);

or

void printArray(const Investment list[], int count);

Both tell the compiler you expect a sequence of Investment objects. Another modern option is:

void printArray(const std::vector<Investment>& list);

Pick one and ensure the function definition uses the exact same signature.

A safer, clearer approach (recommended)
Use std::vector<Investment> and a robust read loop. Example pattern:

std::vector<Investment> investments;
while (inputdata >> name >> amount >> rate >> month >> day >> year) {
    investments.emplace_back(name, amount, rate, month, day, year);
}
printArray(investments); // signature: void printArray(const std::vector<Investment>&)

This avoids fixed-size arrays and common EOF-loop mistakes, and it keeps counts accurate.

Additional practical tips

  • Make the print function take a const parameter to avoid accidental modification.
  • Make getters and printDate const-qualified so they can be called on const objects.
  • Avoid while (!inputdata.eof()) — use while (inputdata >> ...) to detect read failure.
  • Check array bounds (or prefer vector) before writing to InvestmentList[i].
  • Don’t put using namespace std; in headers, and prefer size_t/std::vector::size_type for counts.

The immediate fix is small (change the prototype as suggested). Applying the design tips above will make the program more robust and maintainable for future assignments.

Ok you have to make one change,

in Project2.cpp

.
.
.
using namespace std;

#define N 1000

//void printArray(Investment, int); //HERE
//make it this way
void printArray(Investment [], int);


int main()
{
.
.
.
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.