473,396 Members | 1,655 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

C++ constructor help!!

class description (MyString):

class defines a sequence of characters (similar to class string). The implementation must be as an array of characters that are dynamically allocated and resized as necessary. You must use a null character to terminate the string - just like C-strings are null terminated.

deafult constructor:

Expand|Select|Wrap|Line Numbers
  1. /// get_length(char *s) gets length of s before null character. buf is private data member to store array for MyString
  2.  
  3. MyString::MyString(char *s)
  4. {
  5. buf = new char(get_length(s)+1); 
  6.  
  7. for(int i=0;s;++i)
  8. buf[i] = s[i];
  9.  
  10. buf[get_length(s)+1] = '\0';
  11. }
  12.  
Copy constructor


Expand|Select|Wrap|Line Numbers
  1. MyString::MyString(const MyString &s)
  2. {
  3. char *s_arr = s.buf;
  4.  
  5. buf = new char(s.length()+1); ////length() gets length of MyString object before null
  6.  
  7. for(int i=0;s_arr;++i)
  8. {
  9. buf[i] = s.buf[i];
  10. }
  11.  
  12. buf[length()+1] = '\0';
  13. }
  14.  

I'm getting seg faults and errors when I test these constructors. DO you see any errors in my code? I can't figure it out.

Thanks
Oct 6 '11 #1

✓ answered by johny10151981

yes, and there is more
your for condition is it limited at all??? it seems unlimited.

your for loop should be like this
Expand|Select|Wrap|Line Numbers
  1. for(int i=0;s[i];++i) or for(int i=0;s[i]!=0;++i)
  2. or for(int i=0;s;++i,s++)
  3.  

6 2009
johny10151981
1,059 1GB
yes, i see
on line 10, on the first code block,
on line 12, on second code block.

how???
Expand|Select|Wrap|Line Numbers
  1. a[4]="joh";
  2.  
  3. //now you are getting new str with  the same lenght
  4. b[]=new char(a.length()+1);
  5. //so, it has length 4. 
  6. //according to your code
  7. b[a.length()+1]='\0';
  8.  
  9. //what is that mean? simple;
  10. //b[3+1], that is b[0],b[1],b[2],b[3],b[4].. whcih make the length 5, but you have only 4 byte lenght.. 
  11. //that is all
  12.  
  13.  
  14.  
  15.  
Oct 6 '11 #2
I'm sorry, I'm not sure I understand your answer. So what am i doing wrong and how do i fix it
Oct 6 '11 #3
johny10151981
1,059 1GB
you should use this.
Expand|Select|Wrap|Line Numbers
  1. b[a.length()]='\0';
  2.  
Oct 6 '11 #4
Like this???

Expand|Select|Wrap|Line Numbers
  1. MyString::MyString(char *s)
  2. {
  3. buf = new char(get_length(s)+1); 
  4.  
  5. for(int i=0;s;++i)
  6. buf[i] = s[i];
  7.  
  8. buf[get_length(s)] = '\0';
  9. }
  10.  
Oct 6 '11 #5
johny10151981
1,059 1GB
yes, and there is more
your for condition is it limited at all??? it seems unlimited.

your for loop should be like this
Expand|Select|Wrap|Line Numbers
  1. for(int i=0;s[i];++i) or for(int i=0;s[i]!=0;++i)
  2. or for(int i=0;s;++i,s++)
  3.  
Oct 6 '11 #6
ok, maybe I can point you to this line:

buf = new char(get_length(s)+1);

Here you are calling the constructor of a char and assigning it the value of get_length(s) + 1. To allocate an array, you need to use [ and ].

buf = new char[get_length(s)+1];
Oct 7 '11 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Naren | last post by:
Hello All, I get an unexpected behaviour for this class D{ /*something*/ }; class B
11
by: Amadrias | last post by:
Hi all, I am using a class to transport some data over the network. I then added the attribute to the class. My problem is that this class is part of a framework and that I do not want...
15
by: Alfonso Morra | last post by:
Hi, I have some code from an example, that I want to retrofit into my project. The code from the example has the following line: SharedAppenderPtr myAppender( new...
6
by: Ook | last post by:
Can some kind soul explain this line? I'm not quite sure what the different parts do and exactly how it works. public: // Constructors Zoot(int size = 0) : _size(size), _data(_size ? new int...
3
by: anddos | last post by:
ive got this sample from a book i am learning from c++ , and need abit of help on constructor #ifndef CAR_HPP #define CAR_HPP class Car { public: Car(); Car(int initRadioFreq, int...
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
0
by: Eric_Dexter | last post by:
Is there a way to convert from .pdf to hhp so that I can add help files to boa constructor.. adding html to it would also be helpfull http://www.dexrow.com
3
by: SpecialKay | last post by:
I have a shape SquareEx that extends Shape Square. public class SquareEx extends Square { private Color Shadow = Color.RED; SquareEx(int x, int y){ super.setX(x); ...
3
by: izzyk | last post by:
Can someone please show me how I would create these constructors in the source file...my compiler tells me I have errors. I know I set up my main, header, and source file correctly because I am...
0
by: atencorps | last post by:
Hello I have the following code but need some help on it. The idea of the code is the main sections ie Service Management are viewable when the page is loaded and by clicking on the main...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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,...

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.