|
Four programs (listings 19.4, 19.5, 19.6 and 19.7) in the book
"Teach Yourself C++ in 21 Days" by Jesse Liberty and Bradley Jones
do not compile. The first of these programs is as follows:
//Listing 19.4 Using Operator ostream
#include <iostream>
using namespace std;
const int DefaultSize = 10;
class Animal
{
public:
Animal(int);
Animal();
~Animal() {}
int GetWeight() const { return itsWeight; }
void Display() const { cout << itsWeight; }
private:
int itsWeight;
};
Animal::Animal(int weight):
itsWeight(weight)
{}
Animal::Animal():
itsWeight(0)
{}
template <class T> // declare the template and the parameter
class Array // the class being parameterized
{
public:
// constructors
Array(int itsSize = DefaultSize);
Array(const Array &rhs);
~Array() { delete [] pType; }
// operators
Array& operator=(const Array&);
T& operator[](int offSet) { return pType[offSet]; }
const T& operator[](int offSet) const
{ return pType[offSet]; }
// accessors
int GetSize() const { return itsSize; }
// template <class T>
friend ostream& operator<< (ostream&, Array<T>&);
private:
T *pType;
int itsSize;
};
template <class T>
ostream& operator<< (ostream& output, Array<T>& theArray)
{
for (int i = 0; i < theArray.itsSize; i++)
{
output << "[" << i << "] " << theArray[i] << endl;
}
return output;
}
// implementations follow...
// implement the Constructor
template <class T>
Array<T>::Array(int size):
itsSize(size)
{
pType = new T[size];
for (int i = 0; i < size; i++)
pType[i] = 0;
}
// copy constructor
template <class T>
Array<T>::Array(const Array &rhs)
{
itsSize = rhs.GetSize();
pType = new T[itsSize];
for (int i = 0; i < itsSize; i++)
pType[i] = rhs[i];
}
// operator=
template <class T>
Array<T>& Array<T>::operator=(const Array &rhs)
{
if (this == &rhs)
return *this;
delete [] pType;
itsSize = rhs.GetSize();
pType = new T[itsSize];
for (int i = 0; i < itsSize; i++)
pType[i] = rhs[i];
return *this;
}
int main()
{
bool Stop = false; // flag for looping
int offset, value;<pre lang="c++"></pre>
Array<int> theArray;
while (Stop == false)
{
cout << "Enter an offset (0-9) ";
cout << "and a value. (-1 to stop): ";
cin >> offset >> value;
if (offset < 0)
break;
if (offset > 9)
{
cout << "***Please use values between 0 and 9.***\n";
continue;
}
theArray[offset] = value;
}
cout << endl << "Here's the entire array:" << endl;
cout << theArray << endl;
return 0;
}
Attempting to compile this program on Linux with g++ produces the
following message:
g++ -fpermissive list1904.cpp -o List19.4
list1904.cpp:45:52: warning: friend declaration ‘std::ostream& operator<<(std::ostream&, Array<T>&)’ declares a non-template function [-Wnon-template-friend]
list1904.cpp:45:52: note: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here)
/tmp/ccgXupn5.o: In function `main':
list1904.cpp text+0x102): undefined reference to `operator<<(std::ostream&, Array<int>&)'
collect2: error: ld returned 1 exit status
make: *** [List19.4] Error 1
I would very much appreciate any help in resolving this problem.
Thank you very much,
tim_mann
tcnm
-- modified 14-Dec-12 16:09pm.
|
|
|
|
|
Unfortunatel, since you did not format your code block properly (i.e. between <pre> tags) there are some bits missing, so I cannot compile what you have here. Please edit your message add the tags, and format the code, so it shows like :
#include <iostream>
using namespace std;
void ShowError(DWORD dwError)
{
wcout << "Error code: " << dwError << " << endl;
}
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
I did not realize that, when I copied the code onto this forum. everything disappeared when the signs < > appeared. Thus, #include <iostream> appeared only as #include, and <T> or <int> never appeared. I'm trying to find out how to edit the posting.
tcnm
P.S. Apparently this problem occurred because I had not checked the "Treat my content as plain text not as HTML" button. I have now corrected this situation. The listed program is now complete as it appears in the book.
-- modified 14-Dec-12 16:15pm.
|
|
|
|
|
You still have not used <pre> tags around your code, as I suggested in my original question to you.
One of these days I'm going to think of a really clever signature.
|
|
|
|