Re: please help (novice)
From: ranger (sgrine_at_msn.com)
Date: 11/21/04
- Next message: Ioannis Vranos: "Re: New C++. Why the flowing code cannot be compiled?"
- Previous message: ranger: "Re: please help (novice)"
- In reply to: Dave Townsend: "Re: please help (novice)"
- Next in thread: ranger: "Re: please help (novice)"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 21 Nov 2004 02:13:50 -0800
I really appreciate the fact you answered my question!
I'm very new in C++, I'm studying C++ wih numerical applications in my mind..
Still, thanks a lot!!
"Dave Townsend" <datownsend@comcast.net> wrote in message news:<dLudncepcJE5LQLcRVn-uw@comcast.com>...
> The reason your code fails is the following line....
> > if (pt = NULL){
>
> In C and C++ you should use "==" not "=" to compare two
> items, what you have done is assign NULL to pt and loose the
> value of the address of the memory you alloaced.
>
> dave
>
>
> "Alf P. Steinbach" <alfps@start.no> wrote in message
> news:419f9cc5.1626738406@news.individual.net...
> > * ranger:
> > > Hi,
> > >
> > > I'm a beginner with C++, studying on my own from a book and I've
> > > encoutered quite some problems.. If possible, someone out there might
> > > help me out..
> > >
> > > The problem is the following.. I've tried to create a couple of string
> > > class/functions.. and they do compile.. but when I run them.. windows
> > > suddenly reports an error.. As I'm just a beginner in C++, with noone
> > > around me to help.. I'd appreciate if someone might help me out!
> > >
> > > The simple (reduced) code is the following:
> > >
> > >
> > > #include <iostream.h>
> >
> > #include <iostream>
> >
> > <iostream.h> is not a standard C++ header.
> >
> >
> >
> > > #include <stdio.h>
> > > #include <stdlib.h> // for EXIT_SUCCESS
> > > #include <string.h>
> >
> > #include <cstdio>
> > #include <cstdlib>
> > #include <cstring>
> >
> > is the preferred style for these three headers.
> >
> >
> >
> > > class string{
> > > public:
> > > string(void){characters = 0; pt = NULL;}
> >
> > 'void' as argument list a C'ism; don't use it.
> >
> > Use an initializer list instead of assignment,
> >
> > string(): characters(0), pt(NULL) {}
> >
> >
> > > string(char *s);
> >
> > Argument should be const:
> >
> > string( char const* s );
> >
> > You also need a destructor to deallocate the string.
> >
> > And you need an assignment operator to copy the string, and
> > a copy constructor.
> >
> >
> > > private:
> > > int characters;
> > > char *pt;
> > > };
> > >
> > > string::string(char *s){
> >
> > string::string( char const* s )
> >
> > > characters = strlen(s);
> > > pt = new char[characters];
> > > if (pt = NULL){
> >
> > In standard C++ this test will always fail. Operator 'new' reports
> > failure by means of std::bad_alloc exception, not by returning NULL,
> > so the test would always fail even if it correctly reflected what
> > you intended to write. But it doesn't even that: it's an assignment,
> > not a comparision.
> >
> >
> > > cerr << "Failed to allocate string.\n";
> >
> > Here you need to qualify using the 'std' namespace:
> >
> > std::cerr << "Failed to allocate string.\n";
> >
> > But it's not a good idea to do i/o to report failure to the user;
> > instead use exceptions to report the failure up to the caller.
> >
> >
> > > exit(EXIT_FAILURE);
> > > }
> > > memcpy(pt, s, characters);
> >
> > This is OK if, as seems likely, you don't intend to have a terminating
> > zero byte in your string representation. However, this is where you
> > get a crash. That's due to the earlier error noted above.
> >
> > > }
> > >
> > > void main()
> >
> > 'main' must have return type 'int'.
> >
> > > {
> > > string s1("My first string");
> > > }
> >
> > --
> > A: Because it messes up the order in which people normally read text.
> > Q: Why is it such a bad thing?
> > A: Top-posting.
> > Q: What is the most annoying thing on usenet and in e-mail?
- Next message: Ioannis Vranos: "Re: New C++. Why the flowing code cannot be compiled?"
- Previous message: ranger: "Re: please help (novice)"
- In reply to: Dave Townsend: "Re: please help (novice)"
- Next in thread: ranger: "Re: please help (novice)"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|