Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Compiler having trouble

Status
Not open for further replies.

CINC

Programmer
Oct 30, 2001
17
0
0
US
My friend compiled this with Visual Studio, but my compiler has trouble.

So, here is the code, and please don't think I'm being selfish by giving you all of it:

#ifndef STRING2BHEADER_H
#define STRING2BHEADER_H

#include <iostream>
#include <memory>

using namespace std;

typedef unsigned char CHAR;

const int MAX_CAPACITY = 8;

class String2B
{
public:
enum Mode {DECIMAL, HEXADECIMAL};
String2B();
//~String2B();
String2B(char* source);
void set(char* source);
void setLength(int newLength);
int getLength();
int getCapacity();
CHAR& operator[](int index);
String2B& operator=(char* source);
friend ostream& operator<<(ostream& os, String2B& source);
ostream& write(ostream& os, Mode mode=DECIMAL);
istream& read (istream& is);

private:
CHAR stringData[MAX_CAPACITY];
int length;
char convertHexadeToChar(unsigned int x);
void copySource(char* source);
void zort();
};

#endif //STRING2BHEADER_H

//and the .cpp file...

#include &quot;String2BHeader.h&quot;

String2B::String2B()
{
length=0;
}

String2B::String2B(char* source)
{
zort();
copySource(source);
}

void String2B::set(char* source)
{
copySource(source);
}

void String2B::zort()
{
length=0;
}

void String2B::copySource(char* source)
{
char* x = source;
while(*x)
{
++x;
}

length = x-source;

if (length > MAX_CAPACITY)
{
throw &quot;don't do that&quot;;
}

memcpy(stringData, source, length);
}

void String2B::setLength(int newLength)
{
if (newLength<0 || newLength > MAX_CAPACITY)
{
throw &quot;invalid length&quot;;
}

length = newLength;
}

int String2B::getLength()
{
return length;
}

int String2B::getCapacity()
{
return MAX_CAPACITY;
}

CHAR& String2B::eek:perator[](int index)
{
if (index<0 || index>=length)
{
throw &quot;index is bogus&quot;;
}
return stringData[index];
}

String2B& String2B::eek:perator=(char* source)
{
copySource(source);

return *this;
}

ostream& String2B::eek:perator<<(ostream& os, String2B& source)
{
if ((os.flags() & ios::basefield)==ios::hex)
{
String2B::Mode mode = String2B::HEXADECIMAL;
}
else
{
String2B::Mode mode = String2B::DECIMAL;
}

source.write(os, mode);
os.flush();

return os;
}

ostream& String2B::write(ostream& os, Mode mode)
{
int i;

switch(mode)
{
case DECIMAL:
for (i=0; i<length; i++)
{
os << stringData;
}
break;

case HEXADECIMAL:
CHAR left, right;

for (i=0; i<length; i++)
{
left = stringData >> 4;
right = stringData & 0xF;

os << convertHexadeToChar(left)
<< convertHexadeToChar(right);
//this was all one line
}
break;
}

return os;
}

istream& String2B::read(istream& is)
{
int i;
char c;

i = 0;

while(1)
{
is = is.get(c);

if (is.eof() || (int)c=='\n')
{
break;
}
if (i==MAX_CAPACITY)
{
throw &quot;ERROR&quot;;
}
stringData[i++] = c;
}
length = i;

return is;
}

/*
ostream& String2B::writeHex(ostream& os)
{

}
*/

char String2B::convertHexadeToChar(unsigned int x)
{
if (x>15)
{
throw &quot;bad hex val&quot;;
}
if (x<10)
{
return '0' + (char)x;
}
else
{
return 'A' + (char)(x-10);
}
}
 
What exactly is the trouble you are having? Please list any error messages, failures, etc. that you are having.

James P. Cottingham

I am the Unknown lead by the Unknowing.
I have done so much with so little
for so long that I am now qualified
to do anything with nothing.
 
Sorry, I know this was a bad post, but that's okay because I fixed the problem.
It was due to declaring the method as a friend. Then after 1 or 2 errors, I was done.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top