problem with program

I have a fairly simple program that I created using the g++ compiler but keep getting error messages. would anybody be willing to help me, I can send you the code via email or pm. I'm sure there is a simple fix for the error... I believe it has to do with one of my parameters. anybody?
Copy and paste the error message - we'll start from there if you don't want to share your code.

Also, please do not use private messages or emails to solve this problem - you're basically saying "I don't want others with the same problem to know how I fixed it".
ok, here goes my error message and the first part of each code where I think the problem is:

product.h

#ifndef PRODUCT_H
#define PRODUCT_H

#include <iostream>
#include <cstdint>
#include <string>

using namespace std;

class Product {
public:
Product();
Product(Product & product);
Product(string name, uint32_t barCode, double price);

product.cpp

#ifndef PRODUCT_H
#define PRODUCT_H

#include <iostream>
#include <cstdint>
#include <string>

using namespace std;

class Product {
public:
Product();
Product(Product & product);
Product(string name, uint32_t barCode, double price);

test.cpp

#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <product.h>

// #include <product.cpp> // in lieu of makefile

const size_t arraySize = 10;
const size_t numDigits = 2;

Product CopyCheck (Product p) // pass in by value calls CC
{
Product x(p); // initialization calls CC (NOT assignment!)
return x; // return by value calls CC
}

void AssignCheck (const Product& pIn, Product& pOut) // pass in by reference - no copies made
{
pOut = pIn; // calls assignment (not CC)
}

int main()
{
Product p1("hammer", 0xFFFFFFFF, 15.00), p2;
std::cout << " Products after declaration:\n";
std::cout << " p1 = " << p1 << '\n';
std::cout << " p2 = " << p2 << '\n';

p1.SetName("Copy Checker");
p1.SetCost(10.0);
p2.SetName("Assign Checker");
p2.SetCost(20.0);
std::cout << " Products after Set:\n";
std::cout << " p1 = " << p1 << '\n';
std::cout << " p2 = " << p2 << '\n';

Product p3 = CopyCheck(p1);
std::cout << " Products after p3 = CopyCheck(p1):\n";
std::cout << " p1 = " << p1 << '\n';
std::cout << " p3 = " << p3 << '\n';

// AssignCheck(p2, p3);
std::cout << " Products after AssignCheck(p2,p3):\n";
std::cout << " p2 = " << p2 << '\n';
std::cout << " p3 = " << p3 << '\n';

Product p4 ("Transitive Assignment Check", 50, 25.0);
p1 = p2 = p3 = p4;
std::cout << " Products after p1 = p2 = p3 = p4:\n";
std::cout << " p1 = " << p1 << '\n';
std::cout << " p2 = " << p2 << '\n';
std::cout << " p3 = " << p3 << '\n';
std::cout << " p4 = " << p4 << '\n';

Product * parray = new Product [arraySize];
std::cout << " Product Array after declaration:\n";
for (size_t i = 0; i < arraySize; ++i)
{
std::cout << " p[" << std::setw(numDigits) << i << "] = " << parray[i] << '\n';
}

for (size_t i = 0; i < arraySize; ++i)
{
parray[i].SetName("Titanium Hammer");
parray[i].SetBarCode((uint32_t)(17 + i));
parray[i].SetCost((float)((2*17 + i))/2);
}
std::cout << " Product Array after Set:\n";
for (size_t i = 0; i < arraySize; ++i)
{
std::cout << " p[" << std::setw(numDigits) << i << "] = " << parray[i] << '\n';
}

error message

g++47 -std=c++11 -c -Wall -Wextra -I. test1.cpp
test1.cpp: In function âint main()â:
test1.cpp:42:28: error: no matching function for call to âProduct::Product(Product)â
test1.cpp:42:28: note: candidates are:
In file included from test1.cpp:10:0:
./product.h:14:3: note: Product::Product(std::string, uint32_t, double)
./product.h:14:3: note: candidate expects 3 arguments, 1 provided
./product.h:13:3: note: Product::Product(Product&)
./product.h:13:3: note: no known conversion for argument 1 from âProductâ to âProduct&â
./product.h:12:3: note: Product::Product()
./product.h:12:3: note: candidate expects 0 arguments, 1 provided
make: *** [test1.o] Error 1

It looks like my copy constructor is what g++ doesn't like, but I can't figure out what to use?
Last edited on
Your copy constructor is defined too strict. Change it from this:

Product(Product & product);

to this:

Product(Product const &product);
perfect! that did it.... lesson learned, thank you!
Topic archived. No new replies allowed.