-6
#include <iostream>
#include <string>

using FText = std::string;
using Print = std::cout;

int main()
{
    FText Submission1 = 0;
    FText Submission2 = 0;
    FText Operator = "";

    return 0;
}

As you can see, I did:

using FText = std::string;

Why can I not make a substitution for std::cout? The error I get from the above code is: "No type named 'cout' in namespace 'std'".

I am a beginner and I do not like typing all of std::cout. I managed to create an alias for std::string, but cout is not working.

I am using Xcode.

E_net4
  • 27,810
  • 13
  • 101
  • 139
  • 10
    `cout` is an instance, not a type. – πάντα ῥεῖ Aug 30 '18 at 12:27
  • 1
    That sounds like a really bad idea. – Danon Aug 30 '18 at 12:28
  • You can't write the code like this, for the same reason you can't write `int a; using TA = a;` – Algirdas Preidžius Aug 30 '18 at 12:33
  • you could do `auto& Print = std::cout;` but I don't recommend doing that. I would suggest getting used to typing it (it's only 3 characters longer than Print anyway). P.S: `FText Submission1 = 0;` is a pretty bad way to initialize a std::string (in fact im pretty sure its going to crash, at least [it does here](http://coliru.stacked-crooked.com/a/2025ff080aa7a1fc)) – Borgleader Aug 30 '18 at 12:36
  • @Borgleader "_in fact im pretty sure its going to crash_" Undefined behavior is undefined. It might crash, it might not crash, or it might even produce expected results. Anything is possible with undefined behavior. – Algirdas Preidžius Aug 30 '18 at 12:41
  • 1
    "change string", "change cout" These terms do not fit what you are doing, or are at best ambiguous. Please try to use full sentences to describe your goals. Cheers! – Lightness Races in Orbit Aug 30 '18 at 12:43
  • @Borgleader That's not really a crash, or at best it's a crash only because you didn't handle an exception. That being said, it's permitted to, because it's UB. – Lightness Races in Orbit Aug 30 '18 at 12:45
  • @Borgleader Kind of related, if you're interested: https://stackoverflow.com/a/7019483/560648 – Lightness Races in Orbit Aug 30 '18 at 12:45
  • 1
    Being lazy for typing is one big (if not the most frequent) cause for unneccessarily obfuscated code. Writing `std::cout` is the most concice and readable way of writing `std::cout`, consider that there are millions of c++ programmers and every one of them knows what `std::cout` means, but only you know what `Print` is – 463035818_is_not_an_ai Aug 30 '18 at 12:50
  • Why stop at `Print`? `P` is much less typing, your code will run faster too. – john Aug 30 '18 at 13:27
  • This is a really bad idea. You are trying to make C++ look like your last language. That will stop you learning the proper way to use C++. Just bite the bullet and use the normal standard C++ types and objects. By using the standard types as is you will be able to get help from people that understand the language much more easily. – Martin York Aug 30 '18 at 15:37
  • btw @MartinYork c++ is my first language i just think print or P would look alot better –  Sep 02 '18 at 04:42
  • @IsraelProductionzz Let me just state this is a really bad idea. – Martin York Sep 02 '18 at 04:52

3 Answers3

6

You can use using or typedef to introduce a new name for a type.
std::string is a type, and these two are equivalent:

using FText = std::string;
typedef std::string FText;

But as the compiler says, std::cout is not a type (it's a variable).

You can introduce a new name for std::cout using a reference:

int main()
{
    auto& Print = std::cout;
    Print << "Hallo, Word!";
}
molbdnilo
  • 64,751
  • 3
  • 43
  • 82
1

If you don't like look of namespace specifiers, you can do something like this:

int main()
{
    using std::cout;

    cout<< "Hallo, Word!";
}
Swift - Friday Pie
  • 12,777
  • 2
  • 19
  • 42
-1

You can create a new class say print class than override << insertion operator than use object of that class for printing.

class print{
public:
    int a;

    void operator<<(print &obj){
        cout<<obj.a<<endl;
    }

};

print obj;
obj.a =1;
obj<<obj;
D.Malim
  • 86
  • 6