Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm getting errors when I am trying to compile my code. When I write make and click enter it gives me this error. And this is only a small section of the error message.

songs.cpp: In member function ‘bool SongList::addRec(node*, node*)’:
songs.cpp:66:30: error: qualified-id in declaration before ‘(’ token
 const bool SongList::printRec( node *curr){<pre>


Here is my makefile.

C = g++
CPPFLAGS = -Wall -g -std=c++11

main:                   songs.o

main.o:                 songs.h

songs.o:                songs.h

.PHONY: clean
clean:                  # clean the directory
                        $(info -- cleaning the directory --)
                        rm -f *.o
                        rm -f main


C++
//main.cpp
#include "songs.h"
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdlib>
int main()
{
    using namespace std;
    cout << "1";

    //variables
    ifstream inFile;
    SongList list;
    char Med[128];
    char *artist, *songName;
    double length;
    int likes;
    char medium[128];

    //open the song file
    inFile.open("songs.txt");

    if (inFile.fail()) {
        cout << "File failed to open... Check for error and try again.!" << endl;
        return -1;
    }

    //read the file into the linked list
    while (!inFile.eof()) {
        inFile.getline(Med, 128, ',');
        artist = new char[sizeof Med];
        inFile.getline(medium, 128, ',');
        songName = new char[sizeof Med];
        inFile >> length;
        inFile.ignore(',');
        inFile >> likes;
        inFile.ignore('\n');

        list.add(artist, songName, length, likes);
    }

    //Welcome user
    cout << "Welcome to SONGLIST! \nPlease enter your option from the menu. \n"
         << "Press (A) to add a song."
         << "Press (E) to edit the likes of a song.\n"
         << "Press (D) if you want to show all songs.\n"
         << "Press (T) if you want to show songs by artist.\n"
         << "Press (R) to remove all songs with less than m likes.\n";

    return 0;
}


C++
//songs.h
// songs.h
#pragma once
#include <iostream>
#include <fstream>
#include <cstring>

struct node
{
    char* name;
    char* title;
    double length;
    int likes;
    node* next;
};

class SongList
{
public:
    SongList();
    ~SongList();

    bool add(char*&, char*&, double&, int&);
    bool addRec(node* curr, node* data);
    bool editLikes(char*&, char*&, int&);
    const bool showAll();
    const bool showArtist(node* curr, const char*);
    bool removeByLikes(int&);
    bool deleteRec(node*);
    const bool printRec(node*);

private:
    node* head;
    node* temp;
    node* tail;
};



C++
//songs.cpp
// songs.cpp
#include "songs.h"
#include <iostream>
#include <fstream>
#include <cstring>

SongList::SongList()
    : head(nullptr)
    , tail(nullptr)
{
}

SongList::~SongList()
{
}

// adds a song to the list in the correct part according to likes
bool SongList::add(char*& dataName, char*& dataTitle, double& dataLength, int& dataLikes)
{

    node* newNode = new node;

    // set the data
    newNode->name = dataName;
    newNode->title = dataTitle;
    newNode->length = dataLength;
    newNode->likes = dataLikes;

    // check if list is empty, if so this will set the tail+head > new node
    if (head == nullptr) {
        head = newNode;
        tail = newNode;
        return true;
    }

    // Recursion if not head
    return addRec(head, newNode);
}

// Add func help
bool SongList::addRec(node* curr, node* data)
{

    // if the newnode has more likes than head becomes the new head
    if (data->likes >= head->likes) {
        data->next = head;
        head = data;
        return true;
    }

    // if the next song has likes > the data song then -> the data before it
    else if (data->likes >= curr->likes) {
        data->next = curr;
        temp->next = data;
        return true;
    }

    // No inserting end of list then make the data the new tail
    else if (curr == tail) {
        tail->next = curr;
        curr = tail;
        return true;
    }

    // traverse the list if greater likes
    else {
        temp = curr;
        return addRec(curr->next, data);
    }

    // func to print nodes recursively
    const bool SongList::printRec(node * curr)
    {

        // the thing we wanna do
        std::cout << "The name of the artist is" << curr->name << std::endl;
        std::cout << "The title of the song is" << curr->title << std::endl;
        std::cout << "The length of the song is" << curr->length << std::endl;
        std::cout << "The number of likes on the song are" << curr->likes << std::endl;

        // base case
        if (curr == tail)
            return true;

        // recursion
        return printRec(curr->next);
    }

    // Help print func
    const bool SongList::showAll()
    {

        if (head == nullptr) {
            std::cout << "List is empty/n";
            return false;
        }

        return printRec(head);
    }

    // Prints out all songs with data if they have a spesific artist
    const bool SongList::showArtist(node * curr, const char* artist)
    {

        if (curr->name == artist) {
            std::cout << "The name of the artist is" << curr->name << std::endl;
            std::cout << "The title of the is" << curr->title << std::endl;
            std::cout << "The length of the song is" << curr->length << std::endl;
            std::cout << "The number of likes on the song are" << curr->likes << std::endl;
        }

        if (curr == tail) {
            return true;
        }
        return showArtist(curr->next, artist);
    }

    // func to delete all songs from a node
    bool SongList::deleteRec(node * curr)
    {

        if (head == nullptr)
            return false;

        temp = curr;
        delete curr;
        return deleteRec(temp->next);
    }

    // func to remove all songs from the list if it has less anything than M likes
    bool SongList::removeByLikes(int& dataLikes)
    {

        node* travPtr = head;

        while (travPtr->next->likes > dataLikes) {
            travPtr = travPtr->next;
        }
        travPtr = tail;
        return deleteRec(travPtr->next);
    }


What I have tried:

I haven't done much other than looking over main.cpp line by line. I've also done a little googling but IDK what is going on. edit: I changed the makefile to look like this.
output: main.o songs.o
        g++ main.o songs.o -o output
main.o: main.cpp
        g++ -c main.cpp
songs.o: songs.cpp songs.h
        g++ songs.cpp
clean:
        rm main
        rm *.o output
Posted
Updated 27-Apr-21 17:05pm
v4
Comments
Rick York 27-Apr-21 21:23pm    
Indentation is a wonderful thing.
[no name] 27-Apr-21 21:28pm    
Sorry, I'll do that in a second :)
[no name] 27-Apr-21 21:56pm    
I think I indented it. Does it look fine?

1 solution

In your Makefile, you are missing a -c option in the rule for songs.o.

While there's nothing wrong with your Makefile, you are duplicating much of the "magic" that make already knows how to do. You can simplify things a bit like this:
CXXFLAGS = -Wall -Wextra -ggdb 

OBJS = main.o songs.o 

#CPPFLAGS = -I ./include       # assuming local include directory
#LDFLAGS  = -L ./lib           # assuming local lib
#LDLIBS   = -lmylib -lpthread  # add mylib and libptread to compile

output: $(OBJS)
        $(CXX) -ggdb $^ $(LDFLAGS) $(LDLIBS) -o $@

songs.o: songs.cpp songs.h

clean:
        rm -f $(OBJS) output
This should compile your project for you, and as you add new files you will only need to update the OBJS variable, and, of course, add the dependencies of file.h for file.cpp. The make built-in rules will add CXXFLAGS and CPPFLAGS to for the rules to build the .o files for you, so if you need to change the compiler flags, say to add more warnings, or turn on optimization (-O2 eg) , you just need to update the CXXFLAGS definition. Just in case you're not aware, # starts a comment in Make, so the CPPFLAGS, etc are not actually assigned in this example, which means you could leave out the $(LDFLAGS) $(LDLIBS) from the recipe to produce output. I included them here to show how you would use them if they were needed.
 
Share this answer
 
v2
Comments
[no name] 27-Apr-21 21:54pm    
I tried that but I am getting this error.
songs.h:26:2: warning: type qualifiers ignored on function return type [-Wignored-qualifiers]
const bool showAll();
AND
songs.cpp: In member function ‘bool SongList::addRec(node*, node*)’:
songs.cpp:66:30: error: qualified-id in declaration before ‘(’ token
const bool SongList::printRec( node *curr){
jeron1 27-Apr-21 21:58pm    
That method needs a closing brace }.
[no name] 27-Apr-21 22:18pm    
Thank you!
k5054 27-Apr-21 22:21pm    
I'm not sure why you're getting the error in songs.ccp at line 60, since it compiles fine for me, but the warning type qualifiers ignored on function return type is because you have const boolshowAll(). The compiler is telling you that the const qualifier for those functions is being ignored. An integer type is returned as a value, so is in a sense always const as you can't reach into the function and alter the value. The caller of the function gets a copy of the value, and is free to do what it will -- either compare against another value, or store it for later use. So if I had
const int f();
/* ... */
int x = f();
I have assigned the copy of the value returned from f() into variable x, and can now modify x as I please, and it can't affect the return value from f(). If I need x to be unmodified, I have to specify that on the caller side e.g. const int x = f().
[no name] 27-Apr-21 22:23pm    
Ok cool! I'm a newbie so I'm having fun learning the new things :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900