×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Compiler having trouble

Compiler having trouble

Compiler having trouble

(OP)
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 "String2BHeader.h"

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 "don't do that";
  }
  
  memcpy(stringData, source, length);
}  

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

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

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

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

String2B& String2B::operator=(char* source)
{
  copySource(source);
  
  return *this;
}

ostream& String2B::operator<<(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 "ERROR";
    }
    stringData[i++] = c;
  }
  length = i;
  
  return is;
}

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

char String2B::convertHexadeToChar(unsigned int x)
{
  if (x>15)
  {
    throw "bad hex val";
  }
  if (x<10)
  {
    return '0' + (char)x;
  }
  else
  {
    return 'A' + (char)(x-10);
  }
}

RE: Compiler having trouble

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.

RE: Compiler having trouble

(OP)
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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close