Aug 3, 2009 at 4:47pm UTC
I am using C++ Borland Builder for Windows version 6.0 to study the book Thinking in C++ Vol 1. The code below does not run unless I amend
//: C07:MemTest.cpp
// Testing the Mem class
//{L} Mem
#include "Mem.h"
#include "Mem.cpp" ----new line.
I am encountering this problem with all the examples. When I run the code the warning messabe is:
Mem.cpp(6): W8058 Cannot create pre-compiled header: code in header.
I presume the code conflicts with compiler. I would be most grateful for suggestions on how to correct this at minimum cost.
//: C07:Mem.h
#ifndef MEM_H
#define MEM_H
typedef unsigned char byte;
class Mem {
byte* mem;
int size;
void ensureMinSize(int minSize);
public:
Mem();
Mem(int sz);
~Mem();
int msize();
byte* pointer();
byte* pointer(int minSize);
};
#endif // MEM_H ///:~
//: C07:Mem.cpp {O}
344 Thinking in C++ www.BruceEckel.com
#include "Mem.h"
#include <cstring>
using namespace std;
Mem::Mem() { mem = 0; size = 0; }
Mem::Mem(int sz) {
mem = 0;
size = 0;
ensureMinSize(sz);
}
Mem::~Mem() { delete []mem; }
int Mem::msize() { return size; }
void Mem::ensureMinSize(int minSize) {
if(size < minSize) {
byte* newmem = new byte[minSize];
memset(newmem + size, 0, minSize - size);
memcpy(newmem, mem, size);
delete []mem;
mem = newmem;
size = minSize;
}
}
byte* Mem::pointer() { return mem; }
byte* Mem::pointer(int minSize) {
ensureMinSize(minSize);
return mem;
} ///:~
//: C07:MemTest.cpp
// Testing the Mem class
//{L} Mem
#include "Mem.h"
#include <cstring>
#include <iostream>
using namespace std;
class MyString {
Mem* buf;
public:
MyString();
MyString(char* str);
~MyString();
void concat(char* str);
void print(ostream& os);
};
MyString::MyString() { buf = 0; }
MyString::MyString(char* str) {
buf = new Mem(strlen(str) + 1);
strcpy((char*)buf->pointer(), str);
}
void MyString::concat(char* str) {
if(!buf) buf = new Mem;
strcat((char*)buf->pointer(
buf->msize() + strlen(str) + 1), str);
}
346 Thinking in C++ www.BruceEckel.com
void MyString::print(ostream& os) {
if(!buf) return;
os << buf->pointer() << endl;
}
MyString::~MyString() { delete buf; }
int main() {
MyString s("My test string");
s.print(cout);
s.concat(" some additional stuff");
s.print(cout);
MyString s2;
s2.concat("Using default constructor");
s2.print(cout);
} ///:~
Aug 3, 2009 at 4:55pm UTC
#include "Mem.cpp" ----new line.
You are including .cpp file O.O ?
Aug 3, 2009 at 5:12pm UTC
Thank you for your prompt response. Could you please clarify your question?
#include "Mem.cpp" ----new line.
is my only amendment to the code in the book.
Aug 3, 2009 at 5:14pm UTC
You should never include cpp files. cpp files should be linked.
If you #include and link a cpp file you'll get symbol redefinition errors
Aug 3, 2009 at 5:43pm UTC
When I run the code without line #include "Mem.cpp"
I get 6 linker error messages - one for each of the public elements listed in Men.h.
I apologise for my limited knowledge in C++ (I am teaching myself). Could the problem have something to do with the compiler?
Aug 3, 2009 at 5:46pm UTC
Are you compiling and linking all of your cpp files?
Aug 3, 2009 at 5:52pm UTC
I am using C++ Borland Builder for Windows and running MemTest.
Aug 3, 2009 at 6:25pm UTC
After you create your project, you need to go to Project -->Add and add the "Mem.cpp" file to your project.
Aug 3, 2009 at 6:31pm UTC
Thank you very much for your help. It works now.