vendredi 12 mai 2023

difference between introducing using std::cout vs std::string::size_type to a source file

I have been using using std::cout; or using std::cin, etc., to introduce the names successfully to a source file and use them further in the functions of the file. I tried to do the same with std::string::size_type and ended up getting 'error: using-declaration for member at non-class scope' in gcc and 'error: not a valid using-declaration at non-class scope' in MSVC compiler.

Here is the link to the code I have written : https://onlinegdb.com/-c6sJuZdrV.

Following is the relevant excerpt from it.

#include <iostream>
#include <string>

using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::string::size_type;

void Part_2()
{
   string s1, s2;
   cout << "Enter the first string" << endl;
   cin >> s1;
   cout << "Enter the second string" << endl;
   cin >> s2;
   size_type s1Size = s1.size();
   size_type s2Size = s2.size();
}

I expected using std::string::size_type; to work just like using std::cout;. I found this Using declaration for a class member shall be a member declaration (C++2003) related question, but that's for a different use-case.

How can I make my code work?

Aucun commentaire:

Enregistrer un commentaire