Issues with a header file

My program is the start of a "music database" which acts as a terminal. The user will enter in commands to get things done. At the moment, however, I am having an issue with the compiler not recognizing std and the method. My code and errors are below. Everything is being done in repl.it. I believe everything is defined correctly to work and am not sure what the issue is. I would appreciate help to figure out this issue. If there are any suggestions for tips to approach this project, I would appreciate that also.

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  #include <iostream>
#include "commands.h"

// Only for user input
int main() {

    std::string input;
    commands command;

    std::cout<<"Welcome to the music database\n";
    command.initialprint();

    // Main loop
    while(input!="exit") {
        std::cout<<"$ ";
        std::cin>>input;
        command.checkInput(input);
    }
}

commands.h
1
2
3
4
5
6
7
8
#pragma once

class commands {
public:
    void initialprint(); // Instruction that print initially everytime
    void checkInput(std::string input); // Run corresponding operation depending on input
    void printHelp(); // Lists all commands user can do
};


commands.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include "commands.h"
#include <iostream>
#include <string>

void commands::initialprint() {
    std::cout<<"Welcome to the music database\n";
    std::cout<<"Type help to get a list of commands. Type exit to leave program.\n";
}
void commands::checkInput(std::string input ) {
    if(input=="help") {
        printHelp();
    }
}
void commands::printHelp() {
    std::cout<<"This is print help\n";
}


My errors in detail

In file included from commands.cpp:1:
./commands.h:6:21: error: use of undeclared identifier 'std'
void checkInput(std::string input); // Run corresponding operation d...
^
commands.cpp:9:16: error: out-of-line definition of 'checkInput' does not match
any declaration in 'commands'
void commands::checkInput(std::string input ) {
^~~~~~~~~~
2 errors generated.
Last edited on
#include <string> in your header file before you first use std::string as a function argument.
@Ganado

That fixed the issue, thanks. Much simpler than I expected, overlooked it somehow. The only thing now to have answered is the best way to approach my classes. I have a command class for all commands the user will use for interacting with the "database". The database is basically a text file that stores all albums and ratings. All commands to add and remove albums will be stored there. Is there a certain design pattern that works best for this type of project?
You're less likely to get a response if you mark your post as solved, just so you know.

I don't really have specific "design pattern" advice here. I would say just start writing code, and if it gets messy, then ask for advice. I would just expand your checkInput function to add "add" and "remove" functionality, which reads in the file as a series of Albums (e.g. use vector), and adds or removes the specified album, and the re-writes the file to disk. Perhaps each Album contains one or more songs, and each song has a name, other metadata, and a rating?

In the existing code: The use of a class here isn't necessary, because you only have functions within the class. "commands" could just be a namespace.
Last edited on
Topic archived. No new replies allowed.