I'm getting an undeclared error in my main program at line 45. Can anyone find out what is causing it?

Main

#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <vector>

#include "playlist.h"

using namespace std;

int main (int argc, char*argv[])
{
    ifstream fin;
    int lineCounter = 1;
    string parsedLine[NUM_OF_FIELDS];
    vector <Song> song;
    unsigned int i;
    string line;
    
    if (argc != 2)
	{
		cout <<"Wrong number of arguments.\n";
		exit(1);
	}
    //Open file specified on command line.
    fin.open (argv[1]);
    //If it cannot open the file exit the program.
    if (fin.fail())
    {
       cout <<"Could not open the input file. \n";
       exit(1);
    }
    
    getline(fin,line);
	
    while (getline(fin,line ))
	{	
		lineCounter++;
		if(! parseLine(line, parsedLine) )
		{
			cerr <<"Error: invalid line in file " <<argv[1] <<" at line " <<lineCounter <<"\n\n";
		}
		else
		{
            Song newsong(parsedline[TITLE], parsedline[ARTIST], parsedline[ALBUM], parsedline[GENRE], 
                 parsedline[SIZE],parsedline[TIME],parsedline[COMMENT]);
            song.push_back(newsong);
        }
        for (int i = static_cast <int> (ARTIST); i < static_cast<int> (NUM_OF_FIELDS); i++)
		{
			parsedLine[i].clear();
		}
  } 
  
  for (i = 0; i < song.size(); i++)
  {
      song.at(i).print(cout);
  }
  cin.get();	
  return 0;
}

playlist class

#ifndef PLAYLIST_H
#define PLAYLIST_H

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

enum FIELD {TITLE=0, ARTIST, ALBUM, GENRE, SIZE, TIME, COMMENT, NUM_OF_FIELDS};

//This class is used to store the song information information read from the file.
class Song
{
  public:
         Song (string title2, string art, string alb, string gen, string siz, string tim, string comm);
         void print ( ostream & out );
  private:
         string title;
         string artist;
         string album;
         string genre;
         string size;
         string time;
         string comment;
};

bool parseLine(string line, string parsedLine[]);
#endif

playlist.cpp

#include "playlist.h"

Song::Song (string title2, string art, string alb, string gen, string siz,
           string tim, string comm)
           :title(title2),artist(art),album(alb),genre(gen),size(siz),
           time(tim),comment(comm)
{ }


bool parseLine( string line, string parsedLine[] )
{
	int start = 0, end = -1; 		//indecis for start and end of the field on the line
	
	
	if (line.size() <= 2* NUM_OF_FIELDS)
		return false;
	
	for(unsigned int next = static_cast <unsigned int> (TITLE); next < static_cast<unsigned int> (NUM_OF_FIELDS) ; next++)
	{
		start = line.find_first_not_of(" \t", end+1);
		if (start != string::npos )
			end = line.find_first_of("\"", start+1);
		else 
			return false;
			
		if (end != string::npos && line[start] == '"' )
		{
			start++;	
			parsedLine[next] = line.substr(start, end-start);
		}
		else
			return false;
	}
	if( end != line.size()-1  && line.find_first_not_of(" \t\r", end+1) !=string::npos )
		return false;
		
	if (parsedLine[0].size() == 0 || parsedLine[1].size() == 0)
		return false;
	
	//if we got here, the line contains a valid input and parsedLine has values of all the fields
	return true;
}

void contact::print ( ostream & out )
{
	out <<title <<", "<<artist <<", " <<album <<", " <<genre <<", " <<size << ", " <<time << ", " <<comment <<"\n";
 
}

Recommended Answers

All 5 Replies

Are you sure that it is line 45 of the main.cpp file? AFAICT, the problem is on line 45 of playlist.cpp, where you have the print() function incorrectly associated with class 'contact'.

Are you sure that it is line 45 of the main.cpp file? AFAICT, the problem is on line 45 of playlist.cpp, where you have the print() function incorrectly associated with class 'contact'.

Fixed it but still gives me that error.

Ah, sorry, I was convinced that was the source of the issue.

Could you post the exact error message, please? There may be some detail in it that would give the cause for it.

45 C:\Users\C.C\Documents\CS136\playlist\main.cpp `parsedline' undeclared (first use this function)

That's it.

Ah, OK, I see the issue now. On that particular line of code, you consistently mistyped parsedLine as parsedline (with the lowercase 'l') and that's what it is complaining about - as far as the compiler is concerned, parsedLine and parsedline are completely different identifiers.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.