Mail Archives: djgpp/2000/03/05/07:48:11
Jason Green schrieb:
>
> caramaith <caramaith AT myokay DOT net> wrote:
>
> > help, I can't see what I did wrong....
>
> Neither can we! ;-)
>
> Show us the full source code.
> Show us the exact compile line(s).
>
> Also please say which example number this is from the TIC++ book.
it's the CppLib.h, CppLib.cpp and CppLibTest.cpp from Chapter 4 of
Volume One of TIC++. Here they come...
-----------------
//: C04:CppLib.h
// C-like library converted to C++
struct Stash {
int size; // Size of each space
int quantity; // Number of storage spaces
int next; // Next empty space
// Dynamically allocated array of bytes:
unsigned char* storage;
// Functions!
void initialize(int size);
void cleanup();
int add(const void* element);
void* fetch(int index);
int count();
void inflate(int increase);
}; ///:~
-----------------
//: C04:CppLib.cpp {O}
// C library converted to C++
// Declare structure and functions:
#include "CppLib.h"
#include <iostream>
#include <cassert>
using namespace std;
// Quantity of elements to add
// when increasing storage:
const int increment = 100;
void Stash::initialize(int sz) {
size = sz;
quantity = 0;
storage = 0;
next = 0;
}
int Stash::add(const void* element) {
if(next >= quantity) // Enough space left?
inflate(increment);
// Copy element into storage,
// starting at next empty space:
int startBytes = next * size;
unsigned char* e = (unsigned char*)element;
for(int i = 0; i < size; i++)
storage[startBytes + i] = e[i];
next++;
return(next - 1); // Index number
}
void* Stash::fetch(int index) {
// Check index boundaries:
assert(0 <= index);
if(index >= next)
return 0; // To indicate the end
// Produce pointer to desired element:
return &(storage[index * size]);
}
int Stash::count() {
return next; // Number of elements in CStash
}
void Stash::inflate(int increase) {
assert(increase > 0);
int newQuantity = quantity + increase;
int newBytes = newQuantity * size;
int oldBytes = quantity * size;
unsigned char* b = new unsigned char[newBytes];
for(int i = 0; i < oldBytes; i++)
b[i] = storage[i]; // Copy old to new
delete []storage; // Old storage
storage = b; // Point to new memory
quantity = newQuantity;
}
void Stash::cleanup() {
if(storage != 0) {
cout << "freeing storage" << endl;
delete []storage;
}
} ///:~
-----------------
//: C04:CppLibTest.cpp
//{L} CppLib
// Test of C++ library
#include "CppLib.h"
#include "../require.h"
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main() {
Stash intStash;
intStash.initialize(sizeof(int));
for(int i = 0; i < 100; i++)
intStash.add(&i);
for(int j = 0; j < intStash.count(); j++)
cout << "intStash.fetch(" << j << ") = "
<< *(int*)intStash.fetch(j)
<< endl;
// Holds 80-character strings:
Stash stringStash;
const int bufsize = 80;
stringStash.initialize(sizeof(char) * bufsize);
ifstream in("CppLibTest.cpp");
assure(in, "CppLibTest.cpp");
string line;
while(getline(in, line))
stringStash.add(line.c_str());
int k = 0;
char* cp;
while((cp =(char*)stringStash.fetch(k++)) != 0)
cout << "stringStash.fetch(" << k << ") = "
<< cp << endl;
intStash.cleanup();
stringStash.cleanup();
} ///:~
-----------------
...a very easy piece of code in my opinion. the exact compiler error
goes like this:
CppLibTest.cpp(2) Error: undefined reference to 'Stash::initialize(int)'
CppLibTest.cpp(4) Error: undefined reference to 'Stash::add(void
const*)'
CppLibTest.cpp(5) Error: undefined reference to 'Stash::count(void)'
CppLibTest.cpp(8) Error: undefined reference to 'Stash::fetch(int)'
CppLibTest.cpp(12) Error: undefined reference to
'Stash::initialize(int)'
CppLibTest.cpp(17) Error: undefined reference to 'Stash::add(void
const*)'
CppLibTest.cpp(20) Error: undefined reference to 'Stash::fetch(int)'
CppLibTest.cpp(23) Error: undefined reference to 'Stash::cleanup(void)'
CppLibTest.cpp(24) Error: undefined reference to 'Stash::cleanup(void)'
Error: collect2: ld returned 1 exit status
....just referring to all functions called in the CppLibTester.cpp.
Maybe there's something special I have to be aware of when trying to
link c++, but I don't try to link any standard templates or
whatsoever....
greez cara
- Raw text -