Hey guys, I'm sorry to be coming here with all this but I feel like I almost have this cracked but feel completely lost. Any help would greatly be appreciated.
For my assignment, I'm supposed to be creating three files: a makefile, .h , and .cpp
I think I've got the .h and .cpp down but keep getting an error when trying to run the makefile. I am given a test file and must be able to "make test.x" without any issue.
If you have any advice, it would be greatly appreciated. Also please talk to me as if I were an idiot, this is my second programming class and I feel like I'm drowning.
Whenever I run "make test1.x" I get this back:
1 2 3 4 5 6 7
|
(CC) -otest1.x product.o
/bin/sh: -c: line 0: syntax error near unexpected token `-otest1.x'
/bin/sh: -c: line 0: `(CC) -otest1.x product.o'
makefile:13: recipe for target 'test1.x' failed
make: *** [test1.x] Error 1
|
My .h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
/* product.h */
#include <iostream>
#include <cstdlib>
#include <cstdint>
#ifndef _PRODUCT_H
#define _PRODUCT_H
class Product
{
public:
void SetName ( const char* ); // sets the name field
void SetBarCode ( uint32_t ); // sets the bar code field
void SetCost ( float ); // sets the cost field
const char* GetName () const; // returns a const pointer to the name field
uint32_t GetBarCode () const; // returns the bar code by value
float GetCost () const; // returns cost by value
Product(); //name "#" code = 0 cost =0
Product(const char* name, uint32_t code, float cost);
~Product();
Product (const Product& p);
Product& operator= (const Product& p);
private:
char * name_; // the product name
uint32_t code_; // the product bar code
float cost_; // the product cost
};
std::ostream& operator<< (std::ostream& os, const Product& p);
#endif
|
My .cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
|
/*
product.cpp
*/
#include"product.h"
#include <cstring>
void Product::SetName ( const char* name )
{
if (name_ != NULL)
delete [] name_;
size_t size = strlen(name);
name_ = new char [1+size];
name_[size] = '\0';
strcpy(name_, name);
}
void Product::SetBarCode ( uint32_t code )
{
code_ = code;
}
void Product::SetCost ( float cost )
{
cost_ = cost;
}
const char* Product::GetName () const
{
return name_;
}
uint32_t Product::GetBarCode () const
{
return code_;
}
float Product::GetCost () const
{
return cost_;
}
Product::Product() : name_(NULL), code_(0), cost_(0.0)
{
name_ = new char [2];
name_[0] = '#';
name_[1] = '\0';
}
Product::Product(const char* name, uint32_t code, float cost) : name_(NULL), code_(code), cost_(cost)
{
size_t size = strlen(name);
name_ = new char [1+size];
name_[size] = '\0';
strcpy(name_ , name);
}
Product::~Product()
{
if (name_ != NULL)
delete[] name_;
}
Product::Product (const Product& p) : name_(NULL), code_(p.code_), cost_(p.cost_)
{
size_t size = strlen(p.name_);
name_ = new char [1+size];
name_[size] = '\0';
strcpy(name_ , p.name_);
}
Product& Product::operator= (const Product& p)
{
if (this != &p)
{
if (name_ != NULL)
delete [] name_;
size_t size = strlen(p.name_);
name_ = new char [1+size];
name_[size] = '\0';
strcpy(name_ , p.name_);
}
return *this;
}
std::ostream& operator<< (std::ostream& os, const Product& p)
{
os << p.GetName() << '\t'
<< p.GetBarCode() << '\t'
<< p.GetCost ();
return os;
}
|
what I have for my makefile
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
CPP = $(HOME)/cpp
#CC = g++ -std=c++11 -Wall -Wextra -I. -I$(CPP)
CC = clang++ -std=c++11 -I. -I$(CPP) -Weverything -Wno-old-style-cast
CCC = clang++ -std=c++11 -I. -I$(CPP) -Weverything -Wno-old-style-cast -Wno-sign-conversion
# -Weverything -Wno-old-style-cast
all: product.x test1.x test2.x
test1.x: product.o
$ (CC) -otest1.x product.o
test2.x: product.o
$(CC) -otest2.x product.o
product.x: product.o
$(CC) -oproduct.x product.o
product.o: product.h product.cpp
$(CC) -c product.cpp
|
and the test file I am supposed to run without modifying.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
|
/**
test.cpp
test harness for class Product
*/
#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);
void AssignCheck (const Product& pIn, Product& pOut);
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(static_cast<uint32_t>(17 + i));
parray[i].SetCost(static_cast<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';
}
// */
}
|
Once again I really appreciate any help or advice. I've been trying to follow this:
http://www.opussoftware.com/tutorial/tutorial.htm
but so far no luck.