Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
This problem seems to only be present on MacOS, compilation is fine on linux also using clang.
the following code is a simplification but demonstrates the issue,
#include<iostream>
int index = 0;
int main()
{
std::cout << index << std::endl;
}
throws this error on compilation:
main.cpp:2:5: error: redefinition of 'index' as different kind of symbol
int index = 0;
^
/usr/include/strings.h:73:7: note: previous definition is here
char *index(const char *, int) __POSIX_C_DEPRECATED(200112L);
^
main.cpp:5:18: warning: address of function 'index' will always evaluate to
'true' [-Wpointer-bool-conversion]
std::cout << index << std::endl;
~~ ^~~~~
main.cpp:5:18: note: prefix with the address-of operator to silence this warning
std::cout << index << std::endl;
^
&
1 warning and 1 error generated.
These were the compiler arguments used:
clang++ -std=c++11 main.cpp -o test
when removing iostream with stdio or nothing the code compiles as expected. Is their a way to fix this or will I have to rename my variable to avoid this?
I did find this but I am already using the C++11 flag and the -std=c11 flag doesn't seem to be valid for C++ code.
The specific version of clang/xcode you're using happens to include the <strings.h> header when you include <iostream>. <strings.h> provides a function called index() at global scope. Thus, you cannot declare a variable also at global scope with the same name.
Either rename the variable, or move it into main():
#include <iostream>
int main()
{
int index = 0;
std::cout << index << std::endl;
}
This works because when a variable has the same identifier as something else but is in a different scope, it is considered a different entity altogether.
To give you an example on how that works, consider this code:
#include <iostream>
int myVar = 0;
int main()
{
int myVar = 1;
std::cout << myVar << '\n';
std::cout << ::myVar << '\n';
}
This will print:
1
0
because myVar refers to the local variable, but ::myVar to the one at global scope.
Is their a way to fix this or will I have to rename my variable to avoid this?
C++ provides namespaces specifically to avoid collisions between names. You can create one for your variable:
#include<iostream>
namespace MyGlobals {
int index = 0;
}
int main()
{
std::cout << MyGlobals::index << std::endl;
}
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Can you please help me reimplement the most basic functionality of std::cout?
Requirements:
1) C standard headers can be included, but no C++ libraries can be used. For example stdio.h can be used for printf function.
2) It is okay if it only works with one type, let's go with "char*". The objective is to find the briefest implementation.
3) The following line should be working without any modification (namespace should be used):
std::cout << "Hello World!\n";
4) Everything should be within one file including the main() method
Edit:
Here is the code which I have, that results in a compilation error:
#include <stdio.h>
namespace std {
class cout {
public:
void operator << (char* s) {
printf("%s", s);
}
};
}
int main() {
std::cout << "Hello World!\n"; // Compilation error: expected an identifier
}
Thanks
Here is the solution, you were not refering with an operator to the object, but to the class type. You need to create an object of that type to use the << with it.
#include <stdio.h>
//don't ever use std namespace
namespace test {
class base_cout {
public:
void operator << (const char const* s) {
printf("%s",s);
}
};
// This should be extern if you want to use it outside a single file.
base_cout cout;
}
int main() {
test::cout << "Hello World\n";
}
This is what cout in iostream header is:
namespace std { extern ostream __declspec(dllexport) cout; }
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I've been working through the "Programming: Principles and Practice using C++" book, and this example was meant to illustrate how data can be lost in type conversions. But when I tried to execute it, it keeps telling me 'Main' : must return a value. I tried using "return 0;" after the curly brace for the while loop, but then it gave errors around "Unresolved externals".
I've copied the code from the book to Visual Studio and everything else I've copied has worked as expected - would anyone be able to tell me how I could fix this please? I don't know why it's happening with this specific example or how to make it stop asking for a return value.
Sorry if this is dumb, I'm very inexperienced and I've tried googling for an answer, but suggested solutions like "return 0" don't work :)!
#include "std_lib_facilities.h"
int main()
{
double d = 0;
while (cin >> d)
{
int i = d;
char c = i;
int i2 = c;
cout << "d==" << d
<< " i==" << i
<< " i2==" << i2
<< " char(" << c << ")\n";
}
}
I tried using "return 0;" after the curly brace for the while loop, but then it gave errors around "Unresolved externals".
That's a separate, unrelated problem uncovered by fixing the first one. Add the return, then deal with the unresolved externals error.
If that's your whole program, you're unresolved external is most likely iostream. You need to include that and use the correct namespace.
Consider the following code:
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!" << endl;
return 0;
}
Better yet, forgo the using statement and use std::cout so that you don't have to worry about namespace collision. See this question Why is “using namespace std;” considered bad practice?
#include <iostream>
int main() {
std::cout << "Hello World!" << std::endl;
return 0;
}
This is a question regarding the default global namespace in C++. I have the following code that compiles and runs properly using g++ clang-500.2.79.
#include <string>
#include <iostream>
using std::string;
using std::endl;
using std::cout;
bool is_palindrome(const string& str){
return equal(str.begin(), str.end(), str.rbegin());
}
int main(){
cout << "Hello is a palindrome: " << is_palindrome("Hello") << endl;
cout << "madam is a palindrome: " << is_palindrome("madam") << endl;
return 0;
}
My questions is, why does this code compile properly? I forgot to put #include <algorithm> and using std::equal at the beginning of my file. So the expected behaviour is for the compiler to complain.
The example at http://en.cppreference.com/w/cpp/algorithm/equal confirms that I should be using std::equal.
To investigate this further, I tried to track down exactly which version of the equal() function was being called. Being a relative newbie to C++ I don't know exactly how to do this either. I tried,
cout << "The function is: " << equal << endl;
Which generated a compiler error with some interesting information:
/usr/include/c++/4.2.1/bits/stl_algobase.h:771:5:
note: 'std::equal' declared here
Try as I might, I can't find information about stl_algobase (or more probably, I most likely don't understand what I've found). Is stl_algobase a set of functions that are automatically included in the global namespace?
A further questions is: What is the proper way to track (code or otherwise) down which function is being called when you are dealing with potentially overloaded or template functions in C++?
equal is in the std namespace. What you are seeing is argument dependent lookup (ADL). Because the arguments are in the std, the name lookup for equal considers that namespace too.
Here's a simplified example:
namespace foo
{
struct Bar {};
}
namespace foo
{
void bar(const Bar&) {}
void bar(int) {}
}
int main()
{
foo::Bar b;
foo::bar(b); // OK
bar(b); // ADL, OK
foo::bar(42); // OK
bar(42); // No ADL: error: 'bar' was not declared in this scope
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to convert a number to string and vice versa in C++
I am using Qt Creator 2.5.0 and gcc 4.7 (Debian 4.7.2-4). I added "QMAKE_CXXFLAGS += -std=c++11" to .pro file. Everything seems to be OK, I used C++11 std::for_each and so on. But when I included "string" header and wanted to use stoi, i got the following error:
performer.cpp:336: error: 'std::string' has no member named 'stoi'
I found some questions related to MinGW and one more, to Eclipse CDT and they had their answers. But I use Linux, why it is NOT working here?
#include <iostream>
#include <string>
int main()
{
std::string test = "45";
int myint = stoi(test);
std::cout << myint << '\n';
}
or
#include <iostream>
#include <string>
using namespace std
int main()
{
string test = "45";
int myint = stoi(test);
cout << myint << '\n';
}
look at http://en.cppreference.com/w/cpp/string/basic_string/stol
std::stoi is a function at namespace scope, taking a string as its argument:
std::string s = "123";
int i = std::stoi(s);
From the error message, it looks like you expect it to be a member of string, invoked as s.stoi() (or perhaps std::string::stoi(s)); that is not the case. If that's not the problem, then please post the problematic code so we don't need to guess what's wrong with it.
I'm trying to run my very first c++ program in linux (linux mint 8). I use either gcc or g++, both with the same problem: the compiler does not find the library I am trying to import.
I suspect something like I should either copy the iostream.h file (which I don't know where to look for) in the working folder, move my file to compile somewhere else or use an option of some sort.
Thanks for your suggestions.
Here's the gcc command, the c++ code, and the error message:
gcc -o addition listing2.5.c
.
#include <iostream.h>
int Addition(int a, int b)
{
return (a + b);
}
int main()
{
cout << "Resultat : " << Addition(2, 4) << "\n";
return 0;
}
.
listing2.5.c:1:22: error: iostream.h: No such file or directory
listing2.5.c: In function ‘main’:
listing2.5.c:10: error: ‘cout’ undeclared (first use in this function)
listing2.5.c:10: error: (Each undeclared identifier is reported only once
listing2.5.c:10: error: for each function it appears in.)
Now the code compiles, but I cannot run it from the command line using the file name. addition: command not found Any suggestion?
cout is defined in the std:: namespace, you need to use std::cout instead of just cout.
You should also use #include <iostream> not the old iostream.h
use g++ to compile C++ programs, it'll link in the standard c++ library. gcc will not. gcc will also compile your code as C code if you give it a .c suffix. Give your files a .cpp suffix.
please use g++ not gcc to compile it
You need <iostream> not <iostream.h>.
They are also header files not libraries.
Other things to fix, cout should be std::cout and you should use std::endl instead of "\n".
You need <iostream>, <iostream.h> is non-standard too-old header. Try this:
#include <iostream>
int Addition(int a, int b)
{
return (a + b);
}
int main()
{
using namespace std;
cout << "Resultat : " << Addition(2, 4) << "\n";
return 0;
}
If you don't want to use std alongside cout as below-
std::cout << "Hello World";
You can also define std at beginning of program by 'using namespace' keywords as-
#include <iostream >
using namespace std;
int Addition(int a, int b)
{
return (a + b);
}
int main()
{
cout << "Result : " << Addition(2, 4) << "\n";
return 0;
}
Now you need not to write std,everytime you use I/O operations.