Error Cannot Convert Parameter 1

This is a discussion on Error Cannot Convert Parameter 1 within the C++ Programming forums, part of the General Programming Boards category; Code: char (*programs)[31][21]; Can you please explain what this does ? Then explain what this does ? Code: &data.get(programs[lp][0],20); get ...

  1. #31
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    Code:
    char (*programs)[31][21];
    Can you please explain what this does ?

    Then explain what this does ?
    Code:
    &data.get(programs[lp][0],20);
    get method

    Im finding this whole thing abit hard to grasp...

  2. #32
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669
    take it easy on the little guy. he's just a kid
    lmfao


    char (*programs)[31][21];
    this allocates a pointer for an array.

    this loads each word(divided by spaces) of up to 20 characters into the beginning of each array part.

    after that(not shown here) i check to see if it's blank if it is, then i make that part of the array "EMPTYSLOT ".

    How's that?

  3. #33
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    This is your answer
    char (*programs)[31][21];
    this allocates a pointer for an array.
    This is what cat wrote earlier in this thread
    char* programs[30][21];

    This creates 630 pointers (presumably for strings of some kind) but allocates no memory for the strings themselves. Were you trying to allocate memory for 30 strings that are 21 characters long? That isn't what happened.
    I dont think you understand pointers properly.

    The keyword new is used to initialize pointers with memory from free store (a section of memory available to all programs). The syntax looks like the example:

    Example:
    int *ptr = new int;
    Taken from pointer tutorial on this site.

  4. #34
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    ...

    this is how i created the array/pointer
    Code:
    char proglist[30][21];
    char (*programs)[30];
    and I try to allocate space like this...
    Code:
    for(int lp=0;lp<=21;lp++)
    	{
    		programs[lp]=new char[21];
    	}
    I get errors saying that: cannot convert from 'char *' to 'char[21]' for the line when i allocate the space.

  5. #35
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    This should work ..

    Code:
    int main()
    {
        char proglist[30][21];
        char *programs[30];
        
        for(int lp=0;lp<=21;lp++)
        {
           programs[lp]=new char[21];
        }
    
        system("PAUSE");
        return 0;
    }

  6. #36
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    code...

    Code:
    int main()
    {
    	char proglist[31][21];
    	char *programs[31];
    	for(int lp=0;lp<=30;lp++)
    	{
    		programs[lp]=new char[21];
    	}
    	programs=&proglist;
        ifstream data("proglist.txt");
    	for(lp=0;lp<=30;lp++)
    	{
    		data.get(programs[lp], " ");
    		if(charcheck(programs[lp][0]," ")==true)
    		{
    			cout << programs[1];
    			strcpy(programs[lp][0],&"EMPTYSLOT ");
    		}
    	}
    	data.close();
    	cout << programs[1];
    	getch();
    	return 0;
    }
    these are the errors(same problem, can't convert):
    Code:
    D:\Programs\Projects\Corewars\main.cpp(45) : error C2440: '=' : cannot convert from 'char (*)[31][21]' to 'char *[31]'
            There are no conversions to array types, although there are conversions to references or pointers to arrays
    D:\Programs\Projects\Corewars\main.cpp(49) : error C2664: 'class std::basic_istream<char,struct std::char_traits<char> > &__thiscall std::basic_istream<char,struct std::char_traits<char> >::get(char *,int)' : cannot convert parameter 2 from 'char [2
    ]' to 'int'
            This conversion requires a reinterpret_cast, a C-style cast or function-style cast
    D:\Programs\Projects\Corewars\main.cpp(50) : error C2664: 'charcheck' : cannot convert parameter 1 from 'char' to 'char *'
            Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
    D:\Programs\Projects\Corewars\main.cpp(53) : error C2664: 'strcpy' : cannot convert parameter 1 from 'char' to 'char *'
            Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
    Error executing cl.exe.
    
    Corewars.exe - 4 error(s), 0 warning(s)

  7. #37
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    just looking at that part of your code, this works for me:

    Code:
    int main()
    {
    	//char proglist[31][21];
    	char *programs[31];
    	for(int lp=0;lp<=30;lp++)
    	{
    		programs[lp]=new char;
    	}
        programs[1]="hhh";
        ifstream data("proglist.txt");
    
    	cout << programs[1];
     int x;
     cin>>x;
    	return 0;
    }
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  8. #38
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    Try this then...
    Code:
    int main()
    {
      char *programs[5];
      ifstream data("proglist.txt");
      for(int lp=0;lp<=5;lp++)
      {
        cout << "programs["<<lp << "] points to : ";		
        programs[lp]=new char[21];   	
        data.getline(programs[lp], 20, ' ');
        cout << programs[lp] << endl;
      }
    	
      for (int x=0; x <= 5; x++)
       delete programs[x];	
    	
      delete[] programs;
      system("PAUSE");
      return 0;
    }
    Last edited by laasunde; 08-01-2003 at 12:20 PM.

  9. #39
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    ...

    I get the same errors as before... could this be a compiler problem after 4 pages?

    Btw, sorry, I had to go to walmart that was like 10 miles away

    edit: waaaait... hold on
    Last edited by drdroid; 08-01-2003 at 02:57 PM.

  10. #40
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    Update

    Now the program crashes before I can output anything, heres the updated code, fully commented and organized:

    Code:
    int main()
    {
    	//Load the file proglist under the name data
    	ifstream data("proglist.txt");
    
    	//Create then allocate space to the pointer programs
    	//Looping through each individual piece of the array
    	char *programs[30];
    	for(int lp=0;lp<=30;lp++)
    	{
    		programs[lp]=new char[20];
    	}
    
    	//Loop through each peace of the array and load the next word in data to it
    	//Then close the file
    	for(lp=0;lp<=30;lp++)
    	{
    		data.get(programs[lp],20,' ');
    		//Print some parts of the array
    		cout << "Programs[" << lp << "] = " << programs[lp] << endl;
    	}
    	data.close();
    
    	//Loop through and free up the space from the array and then
    	//delete the array
    	for (lp=0; lp <= 30; lp++)
    	{
    		delete programs[lp];	
    	}
    	delete[] programs;
    
    	//Wait for a keystroke and then end
    	getch();
    	return 0;
    }
    I'm getting an Illegal operation.
    Last edited by drdroid; 08-01-2003 at 03:43 PM.

  11. #41
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    ...

    laasunde you there? salem?

  12. #42
    Grammar Police HybridM's Avatar
    Join Date
    Jan 2003
    Posts
    355
    Watch his post count soar!

    I wasn't necessarily referring to your knowledge of pointers before, just your knowledge in general, you clearly have no idea what-so-ever about this program. Try something simpler.
    Thor's self help tip:
    Maybe a neighbor is tossing leaf clippings on your lawn, looking at your woman, or harboring desires regarding your longboat. You enslave his children, set his house on fire. He shall not bother you again.

    OS: Windows XP
    Compiler: MSVC

  13. #43
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    Not sure why you get an illegal operation maybe someone else can spot the problem.

    I do however think you should change your code from
    Code:
    data.get(programs[lp],20,' ');
    to
    Code:
    data.getline(programs[lp], 20, ' ');
    it runs fine on my computer anyways.

    You should try to comment out part of your code so that you can narrow down what code segment thats causing your problem. You could probably start with the middle for loop, just a guess...

  14. #44
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    ...

    I think I pointed it out earlier that it always crashes at the call of data.get(programs[lp],20,' ')

  15. #45
    Peace
    Join Date
    Aug 2001
    Posts
    1,510
    Use getline instead of get as laasunde suggested. Your illegal operation is from here, no doubt:

    delete[] programs;

    You're trying to delete memory that is not yours to delete.
    "There's always another way"
    -lightatdawn (lightatdawn.cprogramming.com)

Page 3 of 4 FirstFirst 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-08-2008, 06:31 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  4. cannot start a parameter declaration
    By Dark Nemesis in forum C++ Programming
    Replies: 6
    Last Post: 09-23-2005, 03:09 PM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21