What would 'std:;' do in c++? - c++

I was recently modifying some code, and found a pre-existing bug on one line within a function:
std:;string x = y;
This code still compiles and has been working as expected.
The string definition works because this file is using namespace std;, so the std:: was unnecessary in the first place.
The question is, why is std:; compiling and what, if anything, is it doing?

std: its a label, usable as a target for goto.
As pointed by #Adam Rosenfield in a comment, it is a legal label name.
C++03 §6.1/1:
Labels have their own name space and do not interfere with other identifiers.

It's a label, followed by an empty statement, followed by the declaration of a string x.

Its a label which is followed by the string

(expression)std: (end of expression); (another expression)string x = y;

The compiler tells you what is going on:
#include <iostream>
using namespace std;
int main() {
std:;cout << "Hello!" << std::endl;
}
Both gcc and clang give a pretty clear warning:
std.cpp:4:3: warning: unused label 'std' [-Wunused-label]
std:;cout << "Hello!" << std::endl;
^~~~
1 warning generated.
The take away from this story: always compile your code with warnings enabled (e.g. -Wall).

Related

decltype was not declared in this scope

I am trying to make a program that reads strings and tells me how many punctuation marks are in it. However, when I try to compile it, it gives
the error, "'decltype' was not declared in this scope'. I have just started
c++ in the last month and am new to its concepts.
I'm using Dev C++ 5.11 as the IDE for the code. The code is from the book
c++ Primer fifth edition on page 92
#include<iostream>
#include<cctype>
#include<string>
using namespace std;
int main() {
string s("Hello World!!!");
decltype(s.size() punct_cnt = 0;
// count the number of punctuation characters in s
for (auto c : s) // for every char in s
if (ispunct(c)) // if the character is punctuation
++punct_cnt;
cout << punct_cnt << " punctuation characters in " << s << endl;
}
I expect it to give the output 3, but it gives the error message, "'decltype' was not declared in this scope'.
decltype was only introduced in C++11, and presumably DevC++ is not instructing the compiler to use c++11 mode. In your compiler options, instruct DevC++ to pass the following command-line flag:
-std=c++11

I am having trouble declaring a string type in c++.

I have included as a header. When I call the string I use std::string to fully specify where string is coming from. The error reads Zach was not declared in this scope. Could it by IDE or Compiler? I use eclipse with g++ on linux Ubuntu. The following is my source code.
#include <iostream>
#include <string>
int main(){
std::string name = Zach;
std::cout << name << std::endl;
}
Actually, it was a simple syntax error. Zach needed quotation marks around it.

Simple Regex Usage in C++

I try to do the most basic regex example in C++ using the default lib, and I keep getting either crashes or incoherent behavior.
// with -std=c++11
#include <regex>
using namespace std;
int main()
{
// Copied from the documentation, this one works
if (std::regex_match ("subject", std::regex("(sub)(.*)") ))
std::cout << "string matched\n";
// The most simple example I could try, crash with "regex_error"
if (std::regex_match ("99", std::regex("[0-9]*") ))
std::cout << "integer matched\n";
}
I've tried multiple syntaxes and flags, but nothing seems to work. Since my code seems to be matching all the examples I can find, I'm struggling to see what I'm missing.
As #Wiktor Stribiżew stated, it was just my compiler being too old. Updating the compiler (from gcc 4.1 to gcc 4.9) solved the problem!

Having problems compiling my first C++ program

I tried compiling the following program on XCode on my Mac, and I get these errors:
*Non-ASCII charactes are not allowed outside of literals and identifiers. Fix it: Delete ""
*Use of undeclared identifier 'Hello'
#include <iostream>
using namespace std;
int main()
{
cout << “Hello there world!”;
return 0;
}
This program is literally verbatim from the textbook, "A First Book of C++: An Introduction to Programming" so I'm not sure why it would not work. Is this a Mac vs. PC issue?
The "pretty quotes" copied from your textbook are not valid characters.
Change:
cout << “Hello there world!”;
// ^ ^ These characters are not correct.
To:
cout << "Hello there world!";
The editor you use to type code must not be one that replaces the characters you type with characters that might look nicer.
It seems that you are using non-ASCII double quotes
try with this string (cut and paste) "Hello there world", note the different leading apostrophes.
Try using the Code::Blocks compiler
http://www.codeblocks.org/

About Use of #include Preprocessor Directive in C++

I stumble on the following compilation error in C++ with g++ compiler:
error on line line 1 with message:
invalid preprocessing directive #a
(with a caret above the character a) which is followed by another,probably consequent, error on line 4 with message:
cout was not declared in this scope.
The editor i am using is Code blocks 10.05 with mingw.I tried removing .h extension from the iostream file include statement;switching among different File encoding options;and replacing the angular bracket with single quotes and double quotes as well.i am stuck on it.Pardon if it is a duplicate(although i went through several already asked questions in relevance).
The following code illustrates the problem:
#‪include ‬<iostream.h>
int main()
{
cout<< "abc"+8;
cout<< "def"+4;
cout<< "ha";
return 0;
}
cout exists within the namespace std
So either
#‪include‬<iostream>
//...
std::cout << "abc" << 8;
//...
or
#‪include‬<iostream>
using namespace std;
//...
or
#‪include‬<iostream>
using std::cout;
//...
I tend to prefer the 1st if I'm only using it once or twice, The second if I'm using a lot of different pieces from a namespace (and only in a cpp file), or the third if I'm only using a piece or 2 from a namespace but using the same couple many times.
Additionally as stated in the comments, don't use the 2nd one in headers. See: "using namespace" in c++ headers
Also, you have an invalid character in your #include. You can see it in a hex editor or Note how stackoverflow doesn't highlight them the same:
#‪include‬<iostream>
#include<iostream>
Fully working code:
#include<iostream>
using std::cout;
int main()
{
cout << "abc" << 8;
cout << "def" << 4;
cout << "ha";
return 0;
}
Produces the following output abc8def4ha after I corrected for trying to add 8 to a char*
You have to use std::cout, which means that the "cout" keyword is part of the standard library.
The "invalid directive" error is caused by some invisible Unicode characters in the #include directive; perhaps you copied this from a website that embedded some formatting characters in the code. They can be seen in the question, if you look at the source in a hex editor. That error should be fixed by deleting and retyping the #include line.
You'll probably have other errors, since the code is fifteen years out of date; most modern compilers don't provide pre-standard libraries. These days, the standard library headers don't have a .h extension:
#include <iostream>
and nearly all the names they declare are scoped inside the std namespace:
std::cout << "ha";
Finally, "abc"+8 doesn't do anything sensible. The string literal is an array of four characters, and +8 tries to give you a pointer to the ninth character, which doesn't exist. The result is undefined behaviour.
If you want to print "abc" followed by "8", then you want:
std::cout << "abc" << 8;
Try using it like this:-
#‪include ‬<iostream>
using std :: cout;
cout is the part of std library
If you've got caret above a, try retyping your #include.
You might accidentally type alternative i which looks similar but has different code.
Suggestions about std:: are only relevant for the second error you're getting.
I also didn't fully understand what you were trying to achieve with "abc"+8.