Thread: Problem allocating class on heap...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    26

    Exclamation Problem allocating class on heap...

    Here's my main function, the problem lies almost in the first line where I delcare an instance of DynamicString on heap.

    If you need more code, just let me know:

    Code:
    #include <fstream>  // This is required for file streams.
    #include <iostream>
    #include "email.h"// This is required for input/output streams. (ie cout)
    
    using namespace std;
    
    int main()
    {
      ifstream emailFile( "Email.txt" ); // Open a file for inputing.
      ofstream fout( "EmailResponse-rjg5021.txt" );
    
    
      while( !emailFile.eof() )
      {
          DynamicString s = new DynamicString(  );
          emailFile.getline( s.Buffer, 1000 );
    
               
          Email anEmail( s.Buffer );
           
    
    	if( anEmail.IsValid() )
        {
            //std:: cout << 1 << std::endl;
    	    fout << 1 << endl;
        }
    	else
        {
            //std:: cout << 0 << std::endl;
    	    fout << 0 << endl;
        }
        s.~DynamicString();
      } 
      
      emailFile.close();   // All done so close the file.
      
      return 0;
    }
    The error is this:

    error C2440: 'initializing' : cannot convert from 'DynamicString *' to 'DynamicString'
    No constructor could take the source type, or constructor overload resolution was ambiguous


    but the constructor is:

    Code:
    DynamicString()
    	{
    		Size = 0;
    		Buffer = NULL;
    	}

    I've tried using the another constructor, but to no avail:

    Code:
    DynamicString(const char* buffer, int bufferSize = 0)
    	{
    		// Size not speciied?
    		if ( bufferSize == 0 )
    			// Figure it out by searching for the end of string
    			bufferSize = GetStringLength(buffer);
    
    		Size = bufferSize;
    		Buffer = new char[Size];
    
    		// Copy buffer
    		Copy(buffer, Buffer, 0, bufferSize);
    	}


    Any ideas?

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    make your s variable a pointer, ie:
    DynamicString * s = new DynamicString;

    also, change this:
    s.~DynamicString();

    to:

    delete s;

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    26
    okay, after doing that and changing all .'s to ->'s, it now gives me a bad ptr error since it initializes it Buffer to NULL (0)...

    How would I do that? I'm editing old code to use heap allocation to get rid of stack, so I have to get rid of stack-allocated buffers and remove string size limitations, which doesn't seem possible...?.

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Get rid of DynamicString, whatever it is, if you can. Try std::string instead.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with friend class declaration in a namespace
    By Angus in forum C++ Programming
    Replies: 2
    Last Post: 12-09-2008, 01:29 PM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. template class default constructor problem
    By kocika73 in forum C++ Programming
    Replies: 3
    Last Post: 04-22-2006, 09:42 PM
  4. static class problem.
    By Sebastiani in forum C++ Programming
    Replies: 3
    Last Post: 10-16-2002, 03:27 PM
  5. Difficulty superclassing EDIT window class
    By cDir in forum Windows Programming
    Replies: 7
    Last Post: 02-21-2002, 05:06 PM