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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String Problems 1

Status
Not open for further replies.

fugigoose

Programmer
Jun 20, 2001
241
US
Ahhh! My program refuses to work. I'm using string.h becuz it makes life so much easier, but at compile time, my program has an exception and the debugger brings up xstring.h and points to some junk. I've had strings work before.....but now it dont wanna work. I hate c++, it's so volatile, friggen link errors here, exceptions there. Then you copy the code and paste it into a new c++ file and it works. It's like what the heck? Anyway, if i can just get this demo program for a friend to work, id be content for a few days (untill i try to do something else and c++ messes up). Heres my program:

#include <iostream.h>
#include <stdlib.h>
#include <string>
using namespace std;
const int c_maxDogs = 25;

//-------------------id holder-----------------
struct id{
int idNum;
int idPos;
};

//--------------Object Class Definition----------------
class object{

public:
string className; //string identifying object type
id id; //Id used to identify object within the objectHandler
object(); //Constructor
};

//Constructor definition
object::eek:bject(){
}

//--------------Dog Class Definition--------------------
class dog:public object{

public:
string name; //name of dog
float weight; //weight of dog
int age; //age of dog
string breed; //breed of dog
bool sex; //sex of dog, 0 male, 1 female
dog(); //Constructor
void bark(); //Makes dog bark
};

//Constructor definition
dog::dog(){
weight = 0;
age = 0;
sex = 0;
className = &quot;dog&quot;;
}

//Bark definition
void dog::bark(){
printf(&quot;Bark!&quot;);
}

//----------------Object Handler Defintion--------------------
class dogHandler{

public:
dog dogList [c_maxDogs]; //array to hold actual objects
id idList [c_maxDogs]; //array to keep track of object id's
int curId; //current id to assign
int count; //number of objects currently added
objectHandler(); //constructor
id generateId(); //generates id
void addObject(dog obj); //adds an object to handler
};

dogHandler::eek:bjectHandler(){
curId = 0;
count = 0;
}

id dogHandler::generateId(){
id tmpId;
tmpId.idNum = curId;
tmpId.idPos = count;
curId++;
return tmpId;
}

void dogHandler::addObject(dog obj){
if (count <= c_maxDogs){
id tId = generateId();
obj.id = tId;
dogList[count] = obj;
idList[count] = tId;
count++;
}
}

//---------------Finally, the test---------------------------

void main(){
dogHandler dogHnd;
dog tmpDog;

tmpDog.age = 3;
tmpDog.breed = &quot;Collie&quot;;
tmpDog.name = &quot;Lassie&quot;;
tmpDog.sex = 1;
tmpDog.weight = 50;
dogHnd.addObject(tmpDog);
dogHnd.dogList[0].bark();
system(&quot;pause&quot;);
}
 
Hi,

I like when someone hates C++, you are right to do this!
By the way i tried to compile you code snippet with g++, and it has few errors. Here is another version that works :

Code:
#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;
const int c_maxDogs = 25;

//-------------------id holder-----------------
typedef struct id{
  int idNum;
  int idPos;
} id_t;

//--------------Object Class Definition----------------
class object{

public:
  string className; //string identifying object type
  id_t id; //Id used to identify object within the objectHandler
  object();  //Constructor
};

//Constructor definition
object::object(){
}

//--------------Dog Class Definition--------------------
class dog:public object{

public:
  string name; //name of dog
  float weight; //weight of dog
  int age; //age of dog
  string breed; //breed of dog
  bool sex; //sex of dog, 0 male, 1 female
  dog(); //Constructor
  void bark(); //Makes dog bark
};

//Constructor definition
dog::dog(){
  weight = 0;
  age = 0;
  sex = 0;
  className = &quot;dog&quot;;
}

//Bark definition
void dog::bark(){
  printf(&quot;Bark!&quot;);
}

//----------------Object Handler Defintion--------------------
class dogHandler{

public:
  dog dogList [c_maxDogs]; //array to hold actual objects
  id idList [c_maxDogs];  //array to keep track of object id's
  int curId; //current id to assign
  int count; //number of objects currently added
  dogHandler(); //constructor
  id generateId(); //generates id
  void addObject(dog obj); //adds an object to handler
};

dogHandler::dogHandler(){
  curId = 0;
  count = 0;
}

id_t dogHandler::generateId(){
  id tmpId;
  tmpId.idNum = curId;
  tmpId.idPos = count;
  curId++;
  return tmpId;
}

void dogHandler::addObject(dog obj){
  if (count <= c_maxDogs){
    id tId = generateId();
    obj.id = tId;
    dogList[count] = obj;
    idList[count] = tId;
    count++;
  }
}

//---------------Finally, the test---------------------------

int main(){
  dogHandler dogHnd;
  dog tmpDog;

  tmpDog.age = 3;
  tmpDog.breed = &quot;Collie&quot;;
  tmpDog.name = &quot;Lassie&quot;;
  tmpDog.sex = 1;
  tmpDog.weight = 50;
  dogHnd.addObject(tmpDog);
  dogHnd.dogList[0].bark();
  system(&quot;pause&quot;);
}

Don't use '#include <iostream.h>' but '#include <iostream>', because it warns you of use of a deprectated header.
Other errors i get when i compiled :

Code:
18: declaration of `id object::id'
8: changes meaning of 'id' from 'struct id'
60: ISO C++ forbids declaration of 'objectHandler' with no type
65: ISO C++ forbids declaration of 'objectHandler' with no type
90: 'main' must return 'int'

--
Globos
 
Use `id_t' each time you use `struct id' in your code. Actually you just type `id', but it's not correct.

--
Globos
 
change #include<iostream.h> to #include<iostream>

You should not (must not) use stl headers with .h extension togever with stl files without extension.

Ion Filipski
1c.bmp

ICQ: 95034075
AIM: IonFilipski
filipski@excite.com
 
Why must I use Id_t in my code? What is Id_t and how is it just sitting after my struct? Is it an alias? You changed 1 of my Id's, but left the others, is there a reason. By the way, my program works now, I appreciate that ;)
 
The Id_t is part of the typedef thingie (not the struct), see doc typdef for more info.

However this is not the problem. Feel free to use your old
struct id{...} thing, without the typedef.

The problem is that you never initialize the dogHandler. Rename the objectHandler method to dogHandler (ie make it the constructor I think that is your intention with it anyway).

Btw, I love C++ and would not blame her for my mistakes...




/Per

&quot;It was a work of art, flawless, sublime. A triumph equaled only by its monumental failure.&quot;
 
I wrote in the corrected version i gave :

Code:
...
typedef struct id{
  int idNum;
  int idPos;
} id_t;

As you said, 'id_t' is a type alias of 'struct id'.
In your code, you used type 'id' instead of 'struct id'.
But you are right, my compiler doesn't warns when just using 'id'. Anyway it's not correct to use just 'id'! So either use the alias 'id_t' or 'struct id'.


--
Globos
 
Globos, your advice about only using &quot;id_t&quot; or &quot;struct id&quot; only applies to C. In C++, &quot;id &quot; should be legal, as well.

Another reason to love or hate C++, whichever side you're on.
 
OK, I forgot this, because I never define structs when programming with C++.

Back to the first version of fugigoose code, he wrote :

Code:
...
//--------------Object Class Definition----------------
class object{

public:
string className; //string identifying object type
id id
Code:
; //Id used to identify object within the objectHandler
object();  //Constructor
};
...

The compiler just didn't allow to use a variable of type 'id' named 'id' here.
g++ compiler tells :
18:declaration of 'id object::id'
8:changes meaning of 'id' from 'struct 'id'

--
Globos
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top