"Variable has initializer but incomplete type" - Help on this error?

This is a discussion on "Variable has initializer but incomplete type" - Help on this error? within the C++ Programming forums, part of the General Programming Boards category; Hi All, When I compile my code I get the following error message: Code: test3.cpp:23: error: variable 'row row_chars' has ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    35

    "Variable has initializer but incomplete type" - Help on this error?

    Hi All,

    When I compile my code I get the following error message:

    Code:
    test3.cpp:23: error: variable 'row row_chars' has initializer but incomplete type
    I think it's due to the order of things - having to make main() at the top of the cpp file (a requirement for my assignment for "good programming").

    I've put all the prototypes at the top, and above all of them I had to make a prototype of my class as well:

    Code:
    using namespace std;
    
    class row;
    void load_maze(list<row> &maze_rows, row &row_chars, const string &filein);
    
    .. etc.
    Then main():

    Code:
    int main (int argc, char *argv[])
    {
       string filein;
       list<row> maze_rows;
       row row_chars(1);
    The last line there is where the error surfaces. When I create a new row object it takes an integer. I tried putting "int number" into the class prototype but that didn't help.

    Any suggestions folks?
    Last edited by abrownin; 04-30-2010 at 11:46 PM. Reason: typo

  2. #2
    Registered User
    Join Date
    Apr 2010
    Posts
    35
    I realise you may like to see all the code, so here it is:

    Code:
    #include <iostream>
    #include <fstream>
    #include <list>
    #include <ctype.h>
    #include <cstdlib>
    
    using namespace std;
    
    class row;
    void load_maze(list<row> &maze_rows, row &row_chars, const string &filein);
    void print_maze(list<row> &maze_rows);
    bool find_char(const char &findData, list<row>::iterator &itr, list<row> &maze_rows);
    int count_char(const char &findData, list<row>::iterator &itr, list<row> &maze_rows);
    bool is_maze_valid(list<row>::iterator &itr, list<row> &maze_rows);
    bool is_outside(const char &findData, list<row>::iterator &itr, list<row> &maze_rows);
    int test_maze(list<row> &maze_rows);
    void calculate_metrics(list<row> &maze_rows);
    
    int main (int argc, char *argv[])
    {
       string filein;
       list<row> maze_rows;
       row row_chars(1);
    	
    	//Make sure a maze file was provided
    	
       if (argc != 2) {
          cout << "You need to provide a maze file\n";
          return 0;
       }
      
      filein = argv[1];
      
      //Loading the file into lists...
      
      load_maze(maze_rows, row_chars, filein);
      
      //Running the tests on the lists...
      
      if(test_maze(maze_rows) == 0)
         exit(0);
        
      print_maze(maze_rows);
      
      //Calculate and print the maze metrics...
      
      calculate_metrics(maze_rows);
      
      return 0;
    }
    
    class row
    {
       private:
    	 list<char> row_chars;
    	 int row_number;
    	 
       public:
         row(int number)
    	 {
    	    row_number = number;
    	 }
    	 
    	 void store_chars(char ch)
    	 {
    	    row_chars.push_back(ch);
         }
    	 
    	 void new_row()
    	 {
    	    row_number++;
         }
    	 
    	 void print_row()
    	 {
    		for(list<char>::iterator list_iter = row_chars.begin(); list_iter != row_chars.end(); list_iter++)
            {
    		   cout << *list_iter;
            }
    	 }	 
    	 
    	 bool is_match(char data)
    	 {		
    		//Return false if it cannot find the provided char in the row
    		
    		for(list<char>::iterator list_iter = row_chars.begin(); list_iter != row_chars.end(); list_iter++)
            {
    		   if(*list_iter == data)
    		      return true;
            }
    		return false;
    	 }
    	 
    	 void clear_row()
    	 {
    	    row_chars.clear();
    	 }
    	 
    	 bool is_char_valid()
    	 {
            //Return true if all the chars match up with one of the specified characters
    		for(list<char>::iterator list_iter = row_chars.begin(); list_iter != row_chars.end(); list_iter++)
            {
    		   if(*list_iter == '#' || *list_iter == 's' || *list_iter == 'f' || *list_iter == ' ' || *list_iter == '\n')
    		   continue;
    		   else
    		   return false;
            }
    		return true;
    	 }
    	 
    	 bool is_outside_maze(char findData)
    	 {
            //Return true if the char is outside the maze
    		int hash_count, s_count, f_count;
    		hash_count = s_count = f_count = 0;
    		
    		for(list<char>::iterator list_iter = row_chars.begin(); list_iter != row_chars.end(); list_iter++)
            {
    		   if(*list_iter == '#')
    		   hash_count++;
    		   if(*list_iter == 's')
    		   s_count++;
    		   if(*list_iter == 'f')
    		   f_count++;
    		   if(check_counts(hash_count, s_count, f_count))
    		   continue;
    		   else
    		   return false;
            }
    		return true;
    	 }
    	 
    	 bool check_counts(int hash, int s, int f)
    	 {
    		if((s > 0 || f > 0) && hash == 0)
    		   return false;
    		else
    		   return true;
    	 }
    };
    
    void load_maze(list<row> &maze_rows, row &row_chars, const string &filein)
    {
      ifstream fin;
      char ch;
      
      //open stream to filein
      fin.open(filein.c_str());
      if (!fin)
      {
        cerr << "Unable to load maze " << filein << " \n";
    	exit(0);
      }
      
      while (!fin.eof())
      {
    	fin.get(ch);
    	row_chars.store_chars(ch);
    	if(ch == '\n')
    	{
    	   maze_rows.push_back(row_chars);
    	   row_chars.clear_row();
    	   row_chars.new_row();
    	}
      }
      
      fin.close();
    }
    
    void print_maze(list<row> &maze_rows)
    {  
       for(list<row>::iterator list_iter = maze_rows.begin(); list_iter != maze_rows.end(); list_iter++)
       {
    	  list_iter->print_row();
       }
    }
    
    bool find_char(const char &findData, list<row>::iterator &itr, list<row> &maze_rows)
    {
       // Check if char is found in list
    
       for (itr = maze_rows.begin(); itr != maze_rows.end(); itr++) 
       {
          if (itr->is_match(findData)) 
    	     return true;
       }
       return false;
    }
    
    int count_char(const char &findData, list<row>::iterator &itr, list<row> &maze_rows)
    {
       // Check how many times the char occurs in the row
    
       int data_count = 0;
       
       for (itr = maze_rows.begin(); itr != maze_rows.end(); itr++) 
       {
          if (itr->is_match(findData))
          data_count++;
       }
       return data_count;
    }
    
    bool is_maze_valid(list<row>::iterator &itr, list<row> &maze_rows)
    {
       // Testing if chars in the maze are valid
    
      for (itr = maze_rows.begin(); itr != maze_rows.end(); itr++) 
       {
          if (itr->is_char_valid())
    	  continue;
    	  else
    	  {
    	     cout << "invalid character in maze\n";
    	     return false;
          }
       }
       return true;
    }
    
    bool is_outside(const char &findData, list<row>::iterator &itr, list<row> &maze_rows)
    {
       // Testing if char is placed inside or outside of the maze
    
      for (itr = maze_rows.begin(); itr != maze_rows.end(); itr++) 
       {
          if (itr->is_outside_maze(findData))
    	  continue;
    	  else
    	  return false;
       }
       return true;
    }
    
    int test_maze(list<row> &maze_rows)
    {
       /*Tests remaining:
    	Test that 's' and 'f' are not declared outside of the maze*/
    	
       //Testing to see if 's' is contained in the maze...
       list<row>::iterator list_iter;
       if(find_char('s', list_iter, maze_rows))
       {
          //Testing to see if 'f' is contained in the maze...
          if(find_char('f', list_iter, maze_rows))
    	  {   
    	     //Testing to see if maze contains valid characters...
    		 if(is_maze_valid(list_iter, maze_rows))
    		 {
    		    //Testing to see if there is only one 's' and 'f' contained in the maze...
    			if(count_char('s', list_iter, maze_rows) == 1 && count_char('f', list_iter, maze_rows) == 1)
    			{
    			   //Testing to see if 's' or 'f' is declared outside of the maze...
    			   if(!is_outside('s', list_iter, maze_rows) && !is_outside('f', list_iter, maze_rows))
    	           return(0);
    			   return(1);
    			   }
    			}
    	  }
       }
       //test failed; return 0
       return(0);
    }
    
    void calculate_metrics(list<row> &maze_rows)
    {
       /*Calculate the total number of branches in the maze*/
       /*Calculate the total number of dead ends in the maze*/
       int branches, dead_ends;
       branches = dead_ends = 0;
       
       cout << "Number of branches in maze is " << branches << "\n";
       cout << "Number of dead-ends in maze is " << dead_ends << "\n";
    }
    Last edited by abrownin; 04-30-2010 at 11:46 PM. Reason: typo

  3. #3
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,648
    At the time you actually use a row object, such as here (where the error is reported):
    Code:
    void load_maze(list<row> &maze_rows, row &row_chars, const string &filein);
    the compiler must know the definition of the class so that it can determine the size and composition of the object.

    You can get rid of this error by moving the class definition before the function prototypes.

  4. #4
    Registered User
    Join Date
    Apr 2010
    Posts
    35
    Thankyou! I moved the class up and defined all it's functions and such later in the code, and it works perfectly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  4. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 09:43 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 12:10 AM

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