-10

I'm using iostream and map. When I try to set the functions, they throw an error.

My code:

#include "string"
#include "iostream"
#include "map"

using namespace std;

class myClass {
    map<string, string> Map;
    Map["Ziv"] = "Sion";

    cout << Map["Ziv"];
};

My error:

error: 'Map' does not name a type
error: 'cout' does not name a type

Why I can't use iostream and cout?

eerorika
  • 232,697
  • 12
  • 197
  • 326
Ziv Sion
  • 165
  • 1
  • 11

5 Answers5

10

Why I can't use iostream and cout?

Because a class cannot (directly) contain expression statements. It can contain only member declarations.

Expression statements can only be within functions. This would be correct for example:

class main {
    map<string, string> Map;

    void example_function() {
        Map["Ziv"] = "Sion";
        cout << Map["Ziv"];
    }
};
eerorika
  • 232,697
  • 12
  • 197
  • 326
  • 3
    @ZivSion _"I am pretty new"_ Then this should interest you: [The Definitive C++ Book Guide and List](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – πάντα ῥεῖ Dec 08 '20 at 20:41
  • 1
    So this is not a good question and should have been closed (or tagged duplicate) but because the OP set a bounty, it now lead to a +9 answer. IMHO This seems like something inherently wrong with the site... – JHBonarius Dec 21 '20 at 10:55
0

you can't have a C++ progran without the main function so please create int main(){}. And change the double quotes in the #include directives to angle brackets like below:

#include <string>
#include <iostream>
#include <map>

using namespace std;

class myClass {
    public:
         int myFunc();
};

myClass :: int myFunc(){
       map<string, string> Map;
       Map["Ziv"] = "Sion";

       cout << Map["Ziv"]; 
}

int main(){
myClass myclass;
myclass.myFunc();

return 0;
}

Please consider i am a beginner and forgive my mistakes if any.

Adityan.P
  • 102
  • 3
  • 10
  • [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Gary Strivin' Dec 19 '20 at 15:52
  • @GaryNLOL ```using namspace std;``` is considered bad practice because when we have a complex program with multiple namespaces ```using namespace std;``` can lead to confusion regarding methods of the ```std``` namespace and other namespaces. – Adityan.P Dec 20 '20 at 05:45
0

In object oriented languages, like C++, you can't write expressions/statements in a class. You may want to use a function. Example:

#include<iostream>
using std::cout;

class Example {
public:
   void sayHello() {
      cout << "Hello!";
   }   
}

int main() {
   new Example().sayHello(); // Prints Hello!
}
teslafan0
  • 25
  • 4
0

You have to enter the cout in a function, inside the class you can't execute functions, you just can describe them or variables / objects.

Max Kofler
  • 86
  • 4
0

Class is an Entity which contains related data members and the operations to modify or access those data members. No expression can be executed within the class. It does not make an sense to do something that you can not access. Because the only way to access and modify something are operations.

Yes in case if you want to print something when the class object is created, you can do that in Constructor. May be this is what you want to do:

#include <map>
#include <iostream>
#include <string>

class myClass {
private:
    std::map<std::string, std::string> Map;
public:
    myClass(const std::string& key, const std::string& value){
        Map[key] = value;
        std::cout << value;
    }
};

int main(){
    myClass cls("Ziv", "Sion");
}
foragerDev
  • 1,307
  • 1
  • 9
  • 22