473,473 Members | 2,126 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

why is it undeclared?

//although i have friendship it give such an error but why?
//error C2065: 'mystr' : undeclared identifier
class CdBox
{
public:
char *pronames[40] ;
int CdNo;

CdBox *next;
vector<string> mystr;

CdBox():mystr(1)
{
next=NULL;
}

friend class List;
void display();
};
class List
{
private:
CdBox *tail,*head,*TOP;
public:
List()
{
TOP=new CdBox;
head=tail=TOP;
TOP->next=NULL;
}
friend class CdBox;
void append(CdBox *);
void load();

};

void List::load()
{
ifstream file("file.txt",ios::in);
char beb[100];

while(!file.eof())
{

CdBox *ptr;
ptr=new CdBox;
file.getline(beb,200,'#');
mystr.push_back(beb);
}
}

Nov 22 '05 #1
2 2090
berkay wrote:
//although i have friendship it give such an error but why?
//error C2065: 'mystr' : undeclared identifier
class CdBox
{
public:
char *pronames[40] ;
int CdNo;

CdBox *next;
vector<string> mystr;

CdBox():mystr(1)
{
next=NULL;
}

friend class List;
void display();
};
class List
{
private:
CdBox *tail,*head,*TOP;
public:
List()
{
TOP=new CdBox;
head=tail=TOP;
TOP->next=NULL;
}
friend class CdBox;
void append(CdBox *);
void load();

};

void List::load()
{
ifstream file("file.txt",ios::in);
char beb[100];

while(!file.eof())
{

CdBox *ptr;
ptr=new CdBox;
file.getline(beb,200,'#');
mystr.push_back(beb);
}
}


Friendship has nothing to do with whether something is declared or not.
Friendship is about allowing one class access to something that is
declared private in another class.

I expect that what you are trying to do is this

ptr->mystr.push_back(beb);

or maybe this

head->mystr.push_box(beb);

or this

tail->mystr.push_box(beb);

or this

TOP->mystr.push_box(beb);

I can see four different CdBox pointers that you might be interested in.
Decide which one it is and tell the compiler which one you mean.

john
Nov 22 '05 #2

"berkay" <be**********@gmail.com> wrote in message
news:11*********************@g49g2000cwa.googlegro ups.com...
| //although i have friendship it give such an error but why?
| //error C2065: 'mystr' : undeclared identifier
| class CdBox
| {
| public:
| char *pronames[40] ;
| int CdNo;
|
| CdBox *next;
| vector<string> mystr;
|
| CdBox():mystr(1)
| {
| next=NULL;
| }
|
| friend class List;
| void display();
| };
Doesn't makes sense to me that a CDBox should need to know anything
about the container its in. Neither does the List need to know which
CDBox is adjacent to which. If you really need List to encapsulate its
internals as well as have direct access to the CDBox's structure,
then...

#include <iostream>
#include <ostream>
#include <string>
#include <list>
#include <vector>

class List
{
struct CDBox
{
int n;
std::string name;
CDBox(int i, std::string s) : n(i), name(s) { }
CDBox(const CDBox& copy);
{
n = copy.n;
name = copy.name;
}
~CDBox() { }
// missing assignment operator
}; // CDBox
std::list< CDBox > boxes;
public:
List() : boxes() { }
List(const List& copy); // disabled for now
~List() { }
/* member functions */
void push_back( const int n, const std::string& s )
{
boxes.push_back(CDBox(n, s));
}
void display() const
{
std::cout << "This List has " << boxes.size() << " elements:\n";
typedef std::list< CDBox >::const_iterator CD_Iter;
CD_Iter it = boxes.begin();
for (it; it != boxes.end(); ++it)
{
std::cout << "CDBox number " << (*it).n << "\t";
std::cout << "name: " << (*it).name << "\n";
}
}
}; // List

int main()
{
List list;
list.push_back(0, "box1");
list.push_back(1, "box2");

list.display();

return 0;
}

/*
This List has 2 elements:
CDBox number 0 name: box1
CDBox number 1 name: box2
*/

|
| class List
| {
| private:
| CdBox *tail,*head,*TOP;
| public:
| List()
| {
| TOP=new CdBox;
| head=tail=TOP;
| TOP->next=NULL;
| }
| friend class CdBox;
| void append(CdBox *);
| void load();
|
| };
|

If you prefer not to rely on the standard list but wish to construct
your own then i'ld suggest starting off small and simple. CDBox only
holds a single integer and a pointer to next.

Only an instance of the List class needs to know about CDBox's
compostion. So again CDBox is an embedded class of the List class. CDBox
doesn't care nor need to know anything about List. Conversely, List does
not need to know which CDBox is next, it only tracks the head, tail and
maybe its own size. The member function add(..) can only push_back a new
CDBox allocation. Unload() deallocates all. As is, you can't copy this
List.

You can't create a container with responsabilities not clearly defined
between the element's responsibility and the container's responsability.
Every time you add a new feature, you have to carefully breakpoint
through the code to observe that all those pointers get set
appropriately. The more pointers you provide, the higher the cost when
adding features.

#include <iostream>

class List
{
struct CDBox
{
const int number;
CDBox *next;
CDBox(int i, CDBox *p) : number(i), next(p) { }
CDBox(const CDBox& copy); // disabled
~CDBox() { }
} *head, *tail;
int size;
public:
List() : head(0), tail(0), size(0) { }
List(const List& copy); // disabled for now
~List() { }
/* member functions */
void add(const int& n)
{
std::cout << "add(" << n << ")\n";
if(tail != 0)
{
CDBox* box = new CDBox(n, 0);
tail->next = box;
tail = box;
}
else
{
tail = head = new CDBox(n, 0);
}
++size;
}
void unload()
{
std::cout << "\nunload()\n";
if(tail != 0)
{
CDBox* box = head;
do
{
CDBox* temp = box;
box = temp->next;
delete temp;
--size;
} while(box != 0); // or while(size > 0);
head = tail = 0;
}
}
void display() const
{
std::cout << "\n_______________________\t";
std::cout << "display() list: # of elements: " << size << "\n";
std::cout << "head: " << head << "\n";
std::cout << "tail: " << tail << "\n";
if(size > 0)
{
CDBox* temp = head;
do
{
std::cout << "element:" << temp << "\t";
std::cout << "number = " << temp->number;
std::cout << ", next = " << temp->next << "\n";
temp = temp->next;
} while(temp != 0);
}
else
{
std::cout << "List is empty\n";
}
std::cout << "_______________________\n";
}
}; // List

int main()
{
List list;

list.add(0);
list.add(1);
list.add(2);
list.display();

list.unload();
list.display();

return 0;
}

/*
add(0)
add(1)
add(2)

_______________________ display() list: # of elements: 3
head: 007E0700
tail: 007E0D50
element:007E0700 n = 0, next = 007E0E60
element:007E0E60 n = 1, next = 007E0D50
element:007E0D50 n = 2, next = 00000000
_______________________

unload()

_______________________ display() list: # of elements: 0
head: 00000000
tail: 00000000
List is empty
_______________________
*/

Not a trivial task, and i'm sure that if you look hard enough, you'll
find a bug or two.

Probably a worthwhile suggestion considering all the work involved is to
template the List class so that it can store *any* type of element.

class CDBox
{
...
};

template< class Node >
class List
{
Node* head;
Node* tail;
int size;
public:
List() : head(0), tail(0), size(0) { }
~List() { }
/*member functions*/
void add(const Node& node)
{
...
}
void unload()
{
...
}
};

int main()
{
List< CDBox > cd_list;
cd_list.add(CDBox(...));
cd_list.unload();

List< double > d_list;
d_list.add(1.0);
d_list.unload();

return 0;
}
Nov 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Michael Ströder | last post by:
HI! I'm trying to build Python2.4 on a rather old Debian machine. I only have a shell account there. That's why I'm very limited in my actions. Building _socket fails (see below) although I...
2
by: Mike Schinkel | last post by:
I'm trying to set up a Windows 2003 server using ASP, and I am find ASP pages just STOP as if I had called Response.End whenever an undeclared variable is found. If I turn off Option Explicit, it...
3
by: Saurabh Aggrawal | last post by:
Hi, if (iter->m_name.compare(pstrName) == 0) { // Provide our object. *ppunkItem = iter->m_pUnknown; // Addref our object... iter->m_pUnknown->AddRef(); break; }
1
by: STech | last post by:
I have an instance of an XmlDocument. I am using the Load method to load some xml data but get the error message "Reference to undeclared parameter entity". Here is the xml data <data> <items>...
9
by: W. Van Hooste | last post by:
Just starting with C, can somebody explain why this does not work or point me in the right direction? I wrote some tools and did some coding but cant seem to get this one. I DID declare my FILE...
0
by: Stephanie Doherty | last post by:
Hello World, I am trying to use a _spawnl function like this (and I have included the process.h file): _spawnl(_P_WAIT,iporgfile,iporgfile,NULL); It compiles with the following errors: ...
6
by: Peter Rothenbuecher | last post by:
Hello, when I try to compile the following code with g++ -o client client.c #include<sys/socket.h> #include<stdio.h> #include<stdlib.h> #define ADDRESS "mysocket"; #define MAXLEN 200;
7
by: michigaki | last post by:
hello, we are having problems in compiling a 'slightly' altered x264. We are always receiving a 'helloWorld' undeclared (first use this function) error. We may be commiting a very simple error but...
6
by: Alfred | last post by:
On Ubuntu, I've been experimenting with Kazuho Oku's fascinating Linux- based perl/C trick to implement C and C++ as a scripting language... ...
7
by: Adam01 | last post by:
Im using cygwin to test the code of a server I am writing. I've included sys/types.h, sys/socket.h, netdb.h, and arpa/inet.h. And this is the output.. ../../../sockets.cpp: In constructor...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.