Understanding templates in c++ - c++

I am trying run the following program, But it generates compilation error:
#ifndef TEMPLATE_SUM_H_
#define TEMPLATE_SUM_H_
template<typename T>
class sum
{
public:
sum() {
val_1 = 0;
val_2 = 0;
}
sum(T a, T b) {
val_1 = a;
val_2 = b;
}
friend std::ostream& operator<<(std::ostream &, const sum<> &);
private:
T val_1, val_2;
T result() const;
};
#endif
Source file:
include <iostream>
#include "inc/sum.h"
template<typename T>
T sum<T>::result() const {
return (val_1 + val_2);
}
template<typename T>
std::ostream& operator<<(std::ostream& os, const sum<T>& obj) {
//std::ostream& operator<<(std::ostream& os, sum<T>& obj) {
os << obj.result();
return os;
}
int main()
{
sum<int> int_obj(15, 15);
sum<float> float_obj(5.2, 3.5);
std::cout << "result of int = " << int_obj << std::endl;
std::cout << "result of float = " << float_obj << std::endl;
return 0;
}
Compiling with g++ (4.4.3) it generates following error:
In file included from template.cpp:2:
inc/sum.h:18: error: wrong number of template arguments (0, should be 1)
inc/sum.h:5: error: provided for ‘template<class T> class sum’
template.cpp: In function ‘std::ostream& operator<<(std::ostream&, const sum<T>&) [with T = int]’:
template.cpp:20: instantiated from here
template.cpp:5: error: ‘T sum<T>::result() const [with T = int]’ is private
template.cpp:12: error: within this context
template.cpp: In function ‘std::ostream& operator<<(std::ostream&, const sum<T>&) [with T = float]’:
template.cpp:21: instantiated from here
template.cpp:5: error: ‘T sum<T>::result() const [with T = float]’ is private
template.cpp:12: error: within this context
1) Can Anyone please help me in identifying the error ?
Also Please suggest some links where I can find brief absolute details on how to use templates in c++.
2) I read that templatized func/classes declared in header file, and defined separately are prone to linking error. Can anyone explain/elaborate this ?
Is there any possibility of linking error in above example ?
The statement is as below:
"If a template or inline function is declared in a .h file, define it in that same file. The definitions of these constructs must be included into every .cpp file that uses them, or the program may fail to link in some build configurations."
This example can be done in some more easy way, without using overloaded operator etc. But I am trying to learn/practising templates and experimenting some features.

You need to have a separate template definition for the friend function declaration:
template<typename U>
friend std::ostream& operator<<(std::ostream &, const sum<U> &);
friend declarations do not inherit the template parameters of the enclosing class.

A simple sample source, to get started;
Calculator.h
#ifndef CALCULATOR_H
#define CALCULATOR_H
template <class TYPE>
class Calculator{
public:
Calculator();
TYPE Sum(TYPE param1, TYPE param2);
};
/**
* To avoid template related compilation error
* when templates are used in header and source files
*
* This class file has been removed from the project-source file.
* However, is present in the project folder
* Gets compiled with the header-file (being included)
*/
#include "Calculator.cpp"
#endif
Calculator.cpp
#include <iostream>
using namespace std;
#include "Calculator.h"
template <class TYPE>
Calculator<TYPE>::Calculator()
{
}
template <class TYPE>
TYPE Calculator<TYPE>::Sum(TYPE param1, TYPE param2){
cout << "Calculator::sum" << endl;
cout << param1 <<endl;
cout << param2 <<endl;
TYPE result = param1 + param2 ;
return result;
}
Main.cpp
#include <iostream>
using namespace std;
#include "Calculator.h"
int main(int argc, const char * argv[]) {
cout << "Hello, Calculator!\n";
Calculator<int> cObj;
int out = cObj.Sum(2,3);
cout << "out : " << out << endl;
Calculator<string> cObjS;
string outS = cObjS.Sum("A", "B");
cout << "outS : " << outS << endl;
cout << "Bye, Calculator!\n";
return 0;
}
Additionally, you can refer to the post, to know about how to keep the template source and header contents, and knowing about how to fix the compilation and linker issues (with reasons).

Related

C++ Error: no matching function for call to ‘simplex615<arbitraryFunc>::amoeba

I'm trying to implement some C++ code to find the maximum of a function using a simplex algoithm. Unfortunately, I have zero experience in C++.
I'm running into this error and can't seem to find a solution from answers to similar questions.
simplex.cpp: In function ‘int main(int, char**)’:
simplex.cpp:22:25: error: no matching function for call to
‘simplex615::amoeba(arbitraryFunc&, double)’
simplex.amoeba(foo, 1e-7);
There is also warning related to the linked file "simplex615.h"
In file included from simplex.cpp:4:0:
simplex615.h:302:6: note: candidate: void simplex615::amoeba(optFunc&, double) [with F = arbitraryFunc]
void simplex615 ::amoeba(optFunc& foo, double tol) {
simplex615.h:302:6: note: no known conversion for argument 1 from ‘arbitraryFunc’ to ‘optFunc&
simplex.cpp
#include <vector>
#include <cmath>
#include <iostream>
#include "simplex615.h"
#define ZEPS 1e-10
// function object used as an argument
class arbitraryFunc {
public:
double operator() (std::vector<double>& x) {
// f(x0,x1) = 100*(x1-x0^2)^2 + (1-x0)^2
return 100*(x[1]-x[0]*x[0])*(x[1]-x[0]*x[0])+(1-x[0])*(1-x[0]);
}
};
int main(int main, char** argv) {
double point[2] = {-1.2, 1};
arbitraryFunc foo;
// initial point to start
// WILL BE DISCUSSED LATER
simplex615 <arbitraryFunc> simplex(point, 2); // create a simplex
simplex.amoeba(foo, 1e-7); // optimize for a function
// print outputs
std::cout << "Minimum = " << simplex.ymin() << ", at ("
<< simplex.xmin()[0] << ", " << simplex.xmin()[1]
<< ")" << std::endl;
return 0;
}
simplex615.h
template <class F> // F is a function object
void simplex615 <F>::amoeba(optFunc& foo, double tol) {
evaluateFunction(foo);
while(true) {
evaluateExtremes();
prepareUpdate();
if ( check_tol(Y[idxHi],Y[idxLo],tol) ) break;
updateSimplex(foo, -1.0); // reflection
if ( Y[idxHi] < Y[idxLo] ) {
updateSimplex(foo, -2.0); // expansion
}
else if ( Y[idxHi] >= Y[idxNextHi] ) {
if ( !updateSimplex(foo, 0.5) ) {
contractSimplex(foo);
}
}
}
}
simplex615.h
class optFunc {
public:
virtual double operator() (std::vector<double>& x) = 0;
};
Link to the complete files simplex.cpp and simplex.h: Source code
Any help will be appreciated. Thanks.
It seems to me that in your simplex615.h you have forgotten to use 'class F' in amoeba method. Just replace optFunc with F and it should fix the problem.
template <class F> // F is a function object
void simplex615 <F>::amoeba(F& foo, double tol) {
...
}
A template class argument in C++ defines a general type which can be replaced when using the template.
Also from this example, you can remove the declaration of optFunc from the header file.

boost test fails to find custom print

For some reason boost::test can't manage to compile the following code
#define BOOST_TEST_MODULE EPUTests
#include <iostream>
#include <boost/test/unit_test.hpp>
using epu8 = uint8_t __attribute__((vector_size(16)));
std::ostream &operator<<(std::ostream &stream, epu8 const &term) {
stream << "[" << unsigned(term[0]);
for (unsigned i = 1; i < 16; ++i)
stream << "," << unsigned(term[i]);
stream << "]";
return stream;
}
bool failtest(epu8 x) { return false; }
//****************************************************************************//
BOOST_AUTO_TEST_SUITE(EPU8_test)
BOOST_AUTO_TEST_CASE(EPU8_equal) {
epu8 x {};
BOOST_CHECK_PREDICATE(failtest, (x));
}
BOOST_AUTO_TEST_SUITE_END()
//****************************************************************************//
The error message is
In file included from /usr/include/boost/test/tools/floating_point_comparison.hpp:21:0,
from /usr/include/boost/test/tools/old/impl.hpp:21,
from /usr/include/boost/test/test_tools.hpp:46,
from /usr/include/boost/test/unit_test.hpp:18,
from boost_test_epu.cpp:4:
/usr/include/boost/test/tools/detail/print_helper.hpp: In instantiation of 'struct boost::test_tools::tt_detail::print_log_value<__vector(16) unsigned char>':
/usr/include/boost/test/tools/detail/print_helper.hpp:178:5: required from 'std::ostream& boost::test_tools::tt_detail::operator<<(std::ostream&, const boost::test_tools::tt_detail::print_helper_t<T>&) [with T = __vector(16) unsigned char; std::ostream = std::basic_ostream<char>]'
/usr/include/boost/test/utils/lazy_ostream.hpp:66:29: required from 'std::ostream& boost::unit_test::lazy_ostream_impl<PrevType, T, StorageT>::operator()(std::ostream&) const [with PrevType = boost::unit_test::lazy_ostream; T = boost::test_tools::tt_detail::print_helper_t<__vector(16) unsigned char>; StorageT = const boost::test_tools::tt_detail::print_helper_t<__vector(16) unsigned char>&; std::ostream = std::basic_ostream<char>]'
boost_test_epu.cpp:29:1: required from here
/usr/include/boost/test/tools/detail/print_helper.hpp:47:5: error: static assertion failed: Type has to implement operator<< to be printable
BOOST_STATIC_ASSERT_MSG( (boost::has_left_shift<std::ostream,T>::value),
I understand that g++ is complaining that my epu8 type is not printable. However I know it is because changing
BOOST_CHECK_PREDICATE(failtest, (x));
by
std::cout << x << std::endl;
BOOST_CHECK(failtest(x));
works as expected and print by vector variable correctly.
The same error is reported with various version of clang++ and g++
What am I doing wrong ?
BOOST::test requires the console output operator<< to be in the std namespace
namespace std
{
ostream &operator<<(...)
...
}

Shared library with template parameters

Created shared library with below file
example.cpp
#include <iostream>
template <typename T>
T Max (T & a, T & b)
{
return a < b ? b:a;
}
I was trying to use above library in my code
test.cpp
#include <stdio.h>
#include <iostream>
using namespace std;
template int Max <int> (int & a, int & b);
template double Max <double> (double & a, double & b);
int main ()
{
int i = 39;
int j = 20;
cout << "Max(i, j): " << Max(i, j) << endl;
double f1 = 13.5;
double f2 = 20.7;
cout << "Max(f1, f2): " << Max(f1, f2) << endl;
return 0;
}
when I compiled above code ,getting the following error
test.cpp:4: error: explicit instantiation of non-template ‘int Max’
test.cpp:4: error: expected ‘;’ before ‘<’ token
test.cpp:5: error: explicit instantiation of non-template ‘double Max’
test.cpp:5: error: expected ‘;’ before ‘<’ token
test.cpp: In function ‘int main()’:
test.cpp:11: error: ‘Max’ was not declared in this scope*
I realize this is a trivial example more for academic purpose than anything else. Otherwise I would recommend scrapping the whole thing and just using std::max from the get-go. The standard library provides a wealth of well-specified and tested functionality for the taking; use it unless you have a damn good reason to reinvent the wheel.
If you seriously want to provide a template declaration of a function in a header, and provide the implementation of said-template in a shared object library, you can do it by using explicit instantiation, which it appears you're attempting. However, your attempt appears to be putting said-same in the wrong module.
One way to do it is as follows:
example.hpp
#ifndef MYLIB_EXAMPLE_HPP
#define MYLIB_EXAMPLE_HPP
// define forward declaration here. no implementation
template<class T> T Max(T lhs, T rhs);
#endif
example.cpp
#include "example.hpp"
// provide implementation here
template<class T>
T Max(T lhs, T rhs)
{
return (lhs < rhs) ? rhs : lhs;
}
// explicit instantiations
template int Max<int>(int,int);
template double Max<double>(double,double);
That's it for the library. An example build using clang would be:
clang++ -std=c++11 -Wall -Wextra -pedantic -fPIC -shared -o libexample.so example.cpp
The resulting shared object library exposes the following symbols:
nm libexample.so
0000000000000f50 T __Z3MaxIdET_S0_S0_
0000000000000f20 T __Z3MaxIiET_S0_S0_
U dyld_stub_binder
so as you can see, they're there in the lib. On to the test program that will consume this library:
test.cpp
#include <iostream>
#include "example.hpp"
int main ()
{
int i = 39;
int j = 20;
std::cout << "Max(i, j): " << Max(i, j) << std::endl;
double f1 = 13.5;
double f2 = 20.7;
std::cout << "Max(f1, f2): " << Max(f1, f2) << std::endl;
return 0;
}
We build it as follows (assuming the library is in the local folder):
clang++ -std=c++11 -Wall -Wextra -pedantic -L. -o test -lexample test.cpp
The resulting program, test, produces the following output:
Max(i, j): 39
Max(f1, f2): 20.7
Honestly, there isn't a ton of value in doing it this way, as any future usages of Max that are not provided in your explicit list will result in linker errors (unless that is the intent, in which case it would do exactly what you're looking for).
My recomendations:
Change example.cpp to header a file, Max is a template function
Remove the forward declarations in the code
Remove #include <stdio.h>, unless that really is used somewhere
example.hpp:
template <typename T>
T Max (T& a, T& b)
{
return a < b ? b : a;
}
test.cpp:
#include <iostream>
using namespace std;
int main ()
{
int i = 39;
int j = 20;
cout << "Max(i, j): " << Max(i, j) << endl;
double f1 = 13.5;
double f2 = 20.7;
cout << "Max(f1, f2): " << Max(f1, f2) << endl;
return 0;
}

C++ Template Class with Template Constructor

I tried to implement Properties in c++. I don't no why but if I want to compile my code there are quite a lot of errors. The main Idea was, that a template class and the tamplate constructor will give the requirement Informations.
I would be grateful if somebody could help me!
Compiling Message:
pi#raspberrypi ~/dev/property $ gcc -std=c++0x -o PropertyTest2 PropertyTest2.cpp
PropertyTest2.cpp:22:16: error: expected ‘;’ at end of member declaration
PropertyTest2.cpp:22:19: error: expected unqualified-id before ‘<’ token
PropertyTest2.cpp: In function ‘int main()’:
PropertyTest2.cpp:34:20: error: use of deleted function ‘PropertyTestClass::PropertyTestClass()’
PropertyTest2.cpp:8:7: error: ‘PropertyTestClass::PropertyTestClass()’ is implicitly deleted because the default definition would be ill-formed:
PropertyTest2.cpp:8:7: error: no matching function for call to ‘Property<int>::Property()’
PropertyTest2.cpp:8:7: note: candidates are:
Property4.cpp:21:2: note: template<int (** G)(), void (** S)(int&)> Property::Property()
Property4.cpp:6:7: note: constexpr Property<int>::Property(const Property<int>&)
Property4.cpp:6:7: note: candidate expects 1 argument, 0 provided
Property4.cpp:6:7: note: constexpr Property<int>::Property(Property<int>&&)
Property4.cpp:6:7: note: candidate expects 1 argument, 0 provided
PropertyTest2.cpp:38:20: error: no matching function for call to ‘Property<int>::Set(int)’
PropertyTest2.cpp:38:20: note: candidate is:
Property4.cpp:30:7: note: void Property<T>::Set(T&) [with T = int]
Property4.cpp:30:7: note: no known conversion for argument 1 from ‘int’ to ‘int&’
Property Class (Property.cpp)
#ifndef __PROPERTY_FH__
#define __PROPERTY_FH__
template <class T>
class Property {
private:
typedef T (*TGetter)(void);
typedef void (*TSetter)(T &);
TGetter Getter;
TSetter Setter;
public:
typedef T type;
template<TGetter *G,
TSetter *S
>
Property() {
this->Getter = G;
this->Setter = S;
}
T Get(void) {
return (this->Getter)();
}
void Set(T &value) {
(this->Setter)(value);
}
};
#endif
Testing file (PropertyTest.cpp):
#ifndef __PROPERTY_TEST_FH__
#define __PROPERTY_TEST_FH__
#include <iostream>
#include "Property.cpp"
class PropertyTestClass {
private:
// ReadWrite Property for age
int _age;
int AgeGetter(void) {
return this->_age;
}
void AgeSetter(int &value) {
this->_age = value;
}
public:
// ReadWrite Property for age
Property<int> age<&PropertyTestClass::AgeGetter, &PropertyTestClass::AgeSetter>;
};
#endif
/**
* Program Entry
**/
int main() {
std::cout << "Property Test Programm\n\n";
PropertyTestClass propTest;
std::cout << "ReadWrite Property for age\n";
propTest.age.Set(5);
std::cout << propTest.age.Get() << "\n";
return 0;
}
Ok, this time fixed all the problems in your code.
Property.cpp:
#ifndef __PROPERTY_FH__
#define __PROPERTY_FH__
#include <boost/function.hpp>
template <class T>
class Property {
private:
typedef boost::function <T()> TGetter;
typedef boost::function <void(const T&)> TSetter;
TGetter Getter;
TSetter Setter;
public:
typedef T type;
Property(TGetter G, TSetter S) {
this->Getter = G;
this->Setter = S;
}
T Get(void) {
return (this->Getter)();
}
void Set(const T &value) {
(this->Setter)(value);
}
};
#endif
PropertyTests.cpp:
#ifndef __PROPERTY_TEST_FH__
#define __PROPERTY_TEST_FH__
#include <iostream>
#include <boost/bind.hpp>
#include "Property.cpp"
class PropertyTestClass {
private:
// ReadWrite Property for age
int _age;
int AgeGetter() {
return this->_age;
}
void AgeSetter(const int &value) {
this->_age = value;
}
public:
// ReadWrite Property for age
Property<int> age;
PropertyTestClass() : age(
boost::bind(&PropertyTestClass::AgeGetter, this),
boost::bind(&PropertyTestClass::AgeSetter, this, _1))
{}
};
#endif
/**
* Program Entry
**/
int main() {
std::cout << "Property Test Programm\n\n";
PropertyTestClass propTest;
std::cout << "ReadWrite Property for age\n";
propTest.age.Set(5);
std::cout << propTest.age.Get() << "\n";
return 0;
}
Output:
$ ./a.out
Property Test Programm
ReadWrite Property for age
5

error on include the fstream.h header

I am reading a book "Data Structures and Algorithms in C++" which was published at 2001, I think the c++ compiler should be changed a lot since that time, because I found the code in the book can not be compiled.
#include <fstream.h>
#include <string.h>
So I Google for the answer, and changed the code to
#include <fstream>
#include <cstring>
using namespace std;
But when I tried to compile the code, well, I got some error I have not ever seen:
oo#oo:~/raf$ g++ database.cpp personal.cpp student.cpp useDatabase.cpp -o useDatabase
In file included from /usr/include/c++/4.6/ios:45:0,
from /usr/include/c++/4.6/istream:40,
from /usr/include/c++/4.6/fstream:40,
from personal.h:4,
from student.h:1,
from student.cpp:1:
/usr/include/c++/4.6/bits/ios_base.h: In copy constructor ‘std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)’:
/usr/include/c++/4.6/bits/ios_base.h:788:5: error: ‘std::ios_base::ios_base(const std::ios_base&)’ is private
/usr/include/c++/4.6/bits/basic_ios.h:64:11: error: within this context
In file included from /usr/include/c++/4.6/istream:41:0,
from /usr/include/c++/4.6/fstream:40,
from personal.h:4,
from student.h:1,
from student.cpp:1:
/usr/include/c++/4.6/ostream: In copy constructor ‘std::basic_ostream<char>::basic_ostream(const std::basic_ostream<char>&)’:
/usr/include/c++/4.6/ostream:57:11: note: synthesized method ‘std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)’ first required here
In file included from student.cpp:1:0:
student.h: In function ‘std::ostream& operator<<(std::ostream&, Student&)’:
student.h:17:39: note: synthesized method ‘std::basic_ostream<char>::basic_ostream(const std::basic_ostream<char>&)’ first required here
student.h:15:18: error: initializing argument 1 of ‘std::ostream& Student::writeLegibly(std::ostream)’
student.cpp: At global scope:
student.cpp:24:10: error: prototype for ‘std::ostream& Student::writeLegibly(std::ostream&)’ does not match any in class ‘Student’
student.h:15:18: error: candidate is: std::ostream& Student::writeLegibly(std::ostream)
student.cpp: In member function ‘std::istream& Student::readFromConsole(std::istream&)’:
student.cpp:34:5: error: ‘cout’ was not declared in this scope
In file included from /usr/include/c++/4.6/ios:45:0,
from /usr/include/c++/4.6/ostream:40,
from /usr/include/c++/4.6/iostream:40,
from useDatabase.cpp:1:
/usr/include/c++/4.6/bits/ios_base.h: In copy constructor ‘std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)’:
/usr/include/c++/4.6/bits/ios_base.h:788:5: error: ‘std::ios_base::ios_base(const std::ios_base&)’ is private
/usr/include/c++/4.6/bits/basic_ios.h:64:11: error: within this context
In file included from /usr/include/c++/4.6/iostream:40:0,
from useDatabase.cpp:1:
/usr/include/c++/4.6/ostream: In copy constructor ‘std::basic_ostream<char>::basic_ostream(const std::basic_ostream<char>&)’:
/usr/include/c++/4.6/ostream:57:11: note: synthesized method ‘std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)’ first required here
In file included from useDatabase.cpp:2:0:
student.h: In function ‘std::ostream& operator<<(std::ostream&, Student&)’:
student.h:17:39: note: synthesized method ‘std::basic_ostream<char>::basic_ostream(const std::basic_ostream<char>&)’ first required here
student.h:15:18: error: initializing argument 1 of ‘std::ostream& Student::writeLegibly(std::ostream)’
oo#oo:~/raf$
I spent a lot of time to Google for the answer, but got even more error. Maybe I should register a Github account and upload my code there.
database.cpp
#include "database.h"
template<class T> Database<T>::Database() {
cout << "File name: ";
cin >> fName;
}
template<class T> void Database<T>::add(T& d){
database.open(fName, ios::in|ios::out|ios::binary);
database.seekp(0, ios::end);
d.writeToFile(database);
database.close();
}
template<class T> void Database<T>::modify(const T& d){
T tmp;
database.open(fName, ios::in|ios::out|ios::binary);
while(!database.eof()){
tmp.readFromFile(database);
if (tmp == d){
cin >> tmp;
database.seekp(-d.size(), ios::cur);
tmp.writeToFile(database);
database.close();
return;
}
}
database.close();
cout << "The record to be modified is not in the database\n";
}
template<class T> bool Database<T>::find(const T& d){
T tmp;
database.open(fName, ios::in|ios::binary);
while(!database.eof()){
tmp.readFromFile(database);
if (tmp == d){
database.close();
return true;
}
}
database.close();
return false;
}
template<class T> ostream& Database<T>::print(ostream& out){
T tmp;
database.open(fName, ios::in|ios::binary);
while(1){
tmp.readFromFile(database);
if (database.eof())
break;
out << tmp << endl;
}
database.close();
return out;
}
template<class T> void Database<T>::run() {
char option[5];
T rec;
cout << "1.Add 2.Find 3.Modify a record 4.Exit\n";
cout << "Enter an option: ";
cin.getline(option, 4);
while (cin.getline(option, 4)){
if (*option == '1'){
cin >> rec;
add(rec);
}
else if (*option == '2'){
rec.readKey();
cout << "The record is ";
if (find(rec) == false)
cout << "not ";
cout << "in the database\n";
}
else if (*option == '3'){
rec.readKey();
modify(rec);
}
else if (*option != '4'){
cout << "Wrong option\n";
}
else return;
cout << *this;
cout << "Enter an option";
}
}
database.h
#ifndef DATABASE
#define DATABASE
#include <fstream>
#include <iostream>
using namespace std;
template<class T> class Database{
public:
Database();
void run();
private:
fstream database;
char fName[20];
ostream& print(ostream&);
void add(T&);
bool find(const T&);
void modify(const T&);
friend ostream& operator<<(ostream& out, Database& db) {
return db.print(out);
}
};
#endif
personal.cpp
#include "personal.h"
#include <iostream>
Personal::Personal() : nameLen(10), cityLen(10) {
name = new char[nameLen + 1];
city = new char[cityLen + 1];
}
Personal::Personal(char *ssn, char *n, char *c, int y, long s) : nameLen(10), cityLen(10) {
name = new char[nameLen + 1];
city = new char[cityLen + 1];
strcpy(SSN, ssn);
strcpy(name, n);
strcpy(city, c);
year = y;
salary = s;
}
void Personal::writeToFile(fstream& out) const {
out.write(SSN, 9);
out.write(name, nameLen);
out.write(city, cityLen);
out.write(reinterpret_cast<const char*>(&year), sizeof(int));
out.write(reinterpret_cast<const char*>(&salary), sizeof(int));
}
void Personal::readFromFile(fstream& in) {
in.read(SSN, 9);
in.read(name, nameLen);
in.read(city, cityLen);
in.read(reinterpret_cast<char *>(&year), sizeof(int));
in.read(reinterpret_cast<char *>(&salary), sizeof(int));
}
void Personal::readKey() {
char s[80];
cout << "Enter SSN: ";
cin.getline(s, 80);
strncpy(SSN, s, 9);
}
ostream& Personal::writeLegibly(ostream& out){
SSN[9] = name[nameLen] = city[cityLen] = '\0';
out << "SSN = " << SSN << ", name = " << name
<< ", city = " << city << ", year = " << year
<< ", salary = " << salary;
return out;
}
istream& Personal::readFromConsole(istream& in){
char s[80];
cout << "SSN: ";
in.getline(s, 80);
strncpy(SSN, s, 9);
cout << "Name: ";
in.getline(s, 80);
strncpy(name, s, nameLen);
cout << "City: ";
in.getline(s, 80);
strncpy(city, s, cityLen);
cout << "Birthyear: ";
in >> year;
cout << "Salary: ";
in >> salary;
in.getline(s, 80); //get '\n'
return in;
}
personal.h
#ifndef PERSONAL
#define PERSONAL
#include <fstream>
#include <cstring>
using namespace std;
class Personal {
public:
Personal();
Personal(char*, char*, char*, int, long);
void writeToFile(fstream&) const;
void readFromFile(fstream&);
void readKey();
int size() const {
return 9 + nameLen + cityLen + sizeof(year) + sizeof(salary);
}
bool operator==(const Personal& pr) const{
return strcmp(pr.SSN, SSN) == 0;
}
protected:
const int nameLen, cityLen;
char SSN[10], *name, *city;
int year;
long salary;
ostream& writeLegibly(ostream&);
friend ostream& operator<<(ostream& out, Personal& pr){
return pr.writeLegibly(out);
}
istream& readFromConsole(istream&);
friend istream& operator>>(istream& in, Personal& pr){
return pr.readFromConsole(in);
}
};
#endif
student.cpp
#include "student.h"
Student::Student() : majorLen(10) {
Personal();
major = new char[majorLen + 1];
}
Student::Student(char *ssn, char *n, char *c, int y, long s, char *m): majorLen(11){
Personal(ssn, n, c, y, s);
major = new char[majorLen + 1];
strcpy(major, m);
}
void Student::writeToFile(fstream& out) const {
Personal::writeToFile(out);
out.write(major, majorLen);
}
void Student::readFromFile(fstream& in) {
Personal::readFromFile(in);
in.read(major, majorLen);
}
ostream& Student::writeLegibly(ostream &out){
Personal::writeLegibly(out);
major[majorLen] = '\0';
out << ", major = " << major;
return out;
}
istream& Student::readFromConsole(istream& in){
Personal::readFromConsole(in);
char s[80];
cout << "Major: ";
in.getline(s, 80);
strncpy(major, s, 9);
return in;
}
student.h
#include "personal.h"
class Student : public Personal {
public:
Student();
Student(char*, char*, char*, int, long, char*);
void writeToFile(fstream&) const;
void readFromFile(fstream&);
int size() const{
return Personal::size() + majorLen;
}
protected:
char *major;
const int majorLen;
ostream& writeLegibly(ostream);
friend ostream& operator<<(ostream& out, Student& sr){
return sr.writeLegibly(out);
}
istream& readFromConsole(istream&);
friend istream& operator>>(istream& in, Student& sr){
return sr.readFromConsole(in);
}
};
useDatabase.cpp
#include <iostream>
#include "student.h"
#include "personal.h"
#include "database.h"
int main(){
Database<Personal> db;
db.run();
}
This was a big — arguably too big — set of files to be analyzed in an SO question. You need to learn some methods of reducing the size of your problem for presentation to SO (or to Tech Support).
One of the first steps in C or C++ is to ensure that the headers you create compile cleanly. If the headers aren't clean, you won't be able to compile the code that uses the headers, so headers must be sorted out first.
To help me, I have a script that I call chkhdr:
#!/bin/sh
# Check whether headers compile
tmp=chkhdr-$$
trap 'rm -f $tmp.?; exit 1' 0 1 2 3 13 15
cat > $tmp.c <<EOF
#include HEADER /* Check self-containment */
#include HEADER /* Check idempotence */
int main(void) { return 0; }
EOF
options=
for file in "$#"
do
case "$file" in
(-*) options="$options $file";;
(*) echo "$file"
${CC:-gcc} $options -DHEADER="\"$file\"" -c $tmp.c
;;
esac
done
rm -f $tmp.?
trap 0
I use it to check that headers are both self-contained and idempotent. A self-contained header can be included without any other headers before it, and it compiles. That means it can be used anywhere its services are needed without further ado. An idempotent header can be included multiple times without causing trouble. (I mainly work in C, hence the default compiler is GCC rather than G++. But I can set CC=g++ in the environment to switch to C++ work.)
Your student.h header was not idempotent; I immediately added the standard stanzas at top and bottom:
#ifndef STUDENT_H_INCLUDED
#define STUDENT_H_INCLUDED
...original contents of student.h...
#endif /* STUDENT_H_INCLUDED */
The detailed choice of guard macro name is up to you; that's the naming scheme I use these days, but there's some merit in using something like an MD5 checksum of an draft of the header to give you a quasi-random guard macro.
The output from compiling the student.h header alone was:
In file included from chkhdr-8120.c:1:
/usr/include/c++/4.2.1/bits/ios_base.h: In copy constructor ‘std::basic_ios<char, std::char_traits<char> >::basic_ios(const std::basic_ios<char, std::char_traits<char> >&)’:
/usr/include/c++/4.2.1/bits/ios_base.h:779: error: ‘std::ios_base::ios_base(const std::ios_base&)’ is private
/usr/include/c++/4.2.1/iosfwd:55: error: within this context
/usr/include/c++/4.2.1/iosfwd: In copy constructor ‘std::basic_ostream<char, std::char_traits<char> >::basic_ostream(const std::basic_ostream<char, std::char_traits<char> >&)’:
/usr/include/c++/4.2.1/iosfwd:64: note: synthesized method ‘std::basic_ios<char, std::char_traits<char> >::basic_ios(const std::basic_ios<char, std::char_traits<char> >&)’ first required here
student.h: In function ‘std::ostream& operator<<(std::ostream&, Student&)’:
student.h:20: note: synthesized method ‘std::basic_ostream<char, std::char_traits<char> >::basic_ostream(const std::basic_ostream<char, std::char_traits<char> >&)’ first required here
student.h:20: error: initializing argument 1 of ‘std::ostream& Student::writeLegibly(std::ostream)’
The last line of the error message points firmly at the issue; the other messages are somewhat tangential, and lead up to the crux message. (Very often, especially in C, the first error is the most significant. It was surprising to me that the last line was the key one.) Changing line 20 of student.h to read:
ostream& writeLegibly(ostream&);
resolved that problem, and the student.h header compiled cleanly. The other headers were also clean. Then it was a simple matter to compile the source files. Only student.cpp had an issue:
student.cpp: In member function ‘std::istream& Student::readFromConsole(std::istream&)’:
student.cpp:34: error: ‘cout’ was not declared in this scope
It sounds like a case of 'should use std::cout instead', but adding that leads to:
student.cpp: In member function ‘std::istream& Student::readFromConsole(std::istream&)’:
student.cpp:34: error: ‘cout’ is not a member of ‘std’
That is fixed by including #include <iostream>. Then the code compiles cleanly under:
g++ -c *.cpp
I can't link; I run into undefined references to the Database code, but that's not surprising.
Summary
The details of the fixes are not incredibly important. What is important is the techniques. The key techniques here are:
Ensure headers work cleanly on their own (chkhdr).
Tackle one file at a time.
Problem minimization.
We should not have had to deal with all the code; you should have been able to isolate the problems much better. This is an important skill in any situation where you're reporting software problems to others. Eliminate the extraneous, and reduce to bare essentials.
the errors report
student.cpp:34:5: error: ‘cout’ was not declared in this scope
I think you need
#include <iostream>
In Student change
ostream& writeLegibly(ostream);
to
ostream& writeLegibly(ostream&);
and that should fix a lot. By missing the ampersand you were trying to copy the stream.
While the compiler have changed C++ and C are backwards compatible. One of the strengths of the language. Anyway I have copied out your code into a VS2010 because it was easier for me to analyse the issues with it.
put the #include < iostream > in your personal.h. It is used by personal.cpp where you have a cout.
The signature in student.h of
ostream& writeLegibly(ostream);
is wrong, it should be:
ostream& writeLegibly(ostream&);
I have moved the implementation code in database.cpp into the database.h. Plain simple cut and paste above the #endif. Due to the template nature the compiler did not like it that it was in a separate implementation cpp.
Hope that helps.