Invalid use of class error

While I understand this is a relatively common error among new users and students of C++, I have had no luck finding out why my specific code is giving me this compile time error.

Here is my specification file code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <string>
class Song
{
public:
    Song(std::string title, std::string artist, std::string album);
    Song();
    std::string GetTitle() const;
    std::string GetArtist() const;
    std::string GetAlbum() const;
    std::string SongDisplay() const;
private:
    std::string Title;
    std::string Artist;
    std::string Album;
};


Here's my implementation file code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//******************************************
// Song Class Implementation File (Song.cpp)
// Casey Ferguson CSC 210 July 26, 2011
// Project 7
// *****************************************

#include "Song.h"
#include <iostream>
#include <string>

using namespace std;

Song::Song(std::string title, std::string artist, std::string album)
{
    Title = title;
    Artist = artist;
    Album = album;
}

//----------------------------------------------------

Song::Song()
{
    Title = "";
    Artist = "";
    Album = "";
}

//----------------------------------------------------

string Song::GetAlbum() const
{
    return Album;
}

//----------------------------------------------------

string Song::GetArtist() const
{
    return Artist;
}

//----------------------------------------------------

string Song::GetTitle() const
{
    return Title;
}

//----------------------------------------------------

string Song::SongDisplay() const
{
    string songInfo;
    songInfo = Title + " : " + Artist + " : " + Album;
    return songInfo;
}


And my main program code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//**********************************************************************************
// Song List Program
// This program inputs information from a file, allows editing to take place, then
// outputs the edited list to the input file.
// Casey Ferguson CSC 210 July 26, 2011
//**********************************************************************************

#include <iostream>
#include <string>
#include <fstream>
#include "Song.h"

using namespace std;
int CheckLibraryLength(ifstream&);                          // Initializes the library file length check function
void ReadSongInfo(ifstream&, Song&);                        // Initializes the function meant to read in each object in the array
void TrimSpaces(string&);                                   // Initializes whitespace trimmer function
const int MAX_SONGS = 200;                                  // Sets max song library size at 200
int main()
{
    ifstream inFile;                                        // Initializes file operations
    ofstream outFile;
    string filename;                                        // Initializes filename variable
    cout << "Song Library File Name: ";                     // Prompts for file name
    cin >> filename;
    inFile.open(filename.c_str());                          // Opens the file
    outFile.open(filename.c_str());
    if (!inFile || !outFile)                                // Checks the state of the file
    {
        cout << "Error opening library file.";
        return 1;
    }
    cout << "There are currently " << CheckLibraryLength(inFile)
         << " songs in the library, out of a maximum of " << MAX_SONGS << " songs.";    // Outputs how many songs there are and the max number allowed
    Song library[MAX_SONGS];
    for (int count = 0; count < MAX_SONGS; count++)         // Loop meant to read in current library
    {
        ReadSongInfo(inFile, library[count]);
    }
    int menuChoice;                                         // Initializes menu number choice
    while (menuChoice != 4)                                 // Allows operation to repeat until cancelled using option 4
    {
        cout << "Library Operation Menu" << endl            // Lists menu options
             << "1. Add a song" << endl
             << "2. Remove a song" << endl
             << "3. Display song list" << endl
             << "4. Quit" << endl
             << "Enter the number of your option (1-4): ";
        cin >> menuChoice;
        if (menuChoice == 1)                                // If 1, meant to allow the addition of a song
        {
            int position;                                   // Initializes the position variable
            position = sizeof(library) + 1;                 // Defines said variable
            string title, artist, album;                    // Initializes variables
            cout << "Enter the Title of the song: ";        // Prompts for title
            getline(cin, title, '\n');                      // Stores line as title
            TrimSpaces(title);                              // Trims any extraneous whitespace characters
            cout << "Enter the Artist: ";                   // Prompt for artist
            getline(cin, artist, '\n');                     // Stores the line as artist
            TrimSpaces(artist);                             // Trims extraneous whitespace
            cout << "Enter the Album: ";                    // Prompts for album
            getline(cin, album, '\n');                      // Stores input line as album
            TrimSpaces(album);                              // Trims the extra whitespace
            library[position].Song(title, artist, album);   // Meant to store title, artist, and album to the position in the array
        }
    }
    inFile.close();                                         // Closes inFile
    outFile.close();                                        // Closes outFile
    return 0;                                               // Proper return code
}

int CheckLibraryLength (ifstream& inFile)                   // Function for checking the length of the input/output file
{
    int count = 0;                                          // Initializes line counter
    string dummy;                                           // Initializes dummy string for input
    while (inFile)                                          // While the file is not in fail state, get a line and increment the counter
    {
        getline(inFile, dummy, '\n');
        count++;
    }
    return count;                                           // Return the number of lines (songs)
}

void ReadSongInfo(ifstream& inFile, Song& library)          // Meant to read in song info from the file and store it in the array of the variables in the class file
{
    string title, artist, album;                            // Initializes the variables
    getline(inFile, title, ':');                            // Inputs all data on line up to the first colon to title
    getline(inFile, artist, ':');                           // Inputs all data on line after first colon to the second colon to artist
    getline(inFile, album, '\n');                           // Inputs all data on line after the second colon to the end line character to album
    TrimSpaces(title);                                      // These run the function to truncate leading and trailing whitespace from the strings
    TrimSpaces(artist);
    TrimSpaces(album);
    library.Song(title, artist, album);                     // Meant to store the info to the proper position based on the loop that executes it.
}

void TrimSpaces(string& str)
{
    size_t startpos = str.find_first_not_of(" \t");         // Finds the first non-whitespace character
    size_t endpos = str.find_last_not_of(" \t");            // Finds last non-whitespace character


    if(( string::npos == startpos ) || ( string::npos == endpos))   // If the string is empty, returns a null string
    {
        str = "";
    }
    else
        str = str.substr( startpos, endpos-startpos+1 );            // Otherwise, returns the string from the first non-whitespace character to the last
}


As you can probably tell, this is for a project in my online course, pulling in data from a specified file and storing song title, artist and album in an array, using class objects.

The error I receive points to lines 63 and 92 in the main function code and says invalid use of 'class Song'.
You cannot call a constructor like a normal function. Objects can be constructed once and only once. Constructing them a second time would have nasty consequences.

1
2
3
Song mysong;  // object constructed here

mysong.Song("foo","bar","baz");  // can't construct again here!  Error! 


It looks like you want some kind of Set function, like SetSong or something:

1
2
3
4
void Song::SetSong(std::string title, std::string artist, std::string album)
{
    // set all memebers here
}


Your constructor could then call SetSong so you don't have to duplicate that code. Then when you want to set all elements of an already constructed object, you can call SetSong instead of trying to reconstruct.
line 34: Song library[MAX_SONGS];
all objects have been constructed here using default constructor

line 63: library[position].Song(title, artist, album);
calling constructor on object that already constructed. You need to assing using '=', i.e.
library[position] = Song(title, artist, album);
the same for line 92.

But there is (at least) another issue in your code
line 52: position = sizeof(library) + 1;
sizeof operator returns the number of bytes occupied by the object. So you will always get sizeof(Song)xMAX_SONGS.
Instead you need to have a counter, initialize it to zero before the loop and increment each time you add a new song.

Use more const references instead of passing string by value. The same for returning from Get* functions. And use initialization list in the constructor.

Good luck!
You can't use a constructor like a normal function. Add a method to set the song's details and use that instead.

Andy

P.S. Is it is possible to call a constructor directly, if you really really need to. But it is intrinsically evil!
Okay, thank you everyone!
Topic archived. No new replies allowed.