#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
#include <iomanip>
#include <cstring>
using namespace std;
int main() {
const int size = 15;
char array1[size];
char array2[size] = "four";
cout << array2[3]='\0' << endl;
return 0;
}
Hello, I am new to C++, I am learning the C-string, my question is why
array2[3] ='\0' can not be use inside the cout, the error I get is:
reference to overloaded function could not be resolved.
It works fine if it's outside of cout. thank you :)
Whenever I use std::string to declare a string variable in c++ it prevents the program from outputting anything. for example:
#include <cstdlib>
#include <iostream>
#include <string>
int main() {
std::cout << "Hello";
// std::string s;
return 0;
}
This will output Hello to the command-line as it should do.
#include <cstdlib>
#include <iostream>
#include <string>
int main() {
std::cout << "Hello";
std::string s;
return 0;
}
This will not output anything (and no errors) since i'm declaring a variable using std::string
I'm using the minGW compiler on a Windows 10 64bit machine
I ran this code on a different IDE and it was successful. For some reason I get the above error message on Xcode. I assume I'm missing a header of some kind, but I'm not sure which one.
#include <iostream>
#include <limits>
#include <string>
#include <vector>
int main() {
vector<string> listRestaurants; // error: Implicit instantiation of undefined template
return 0;
}
Xcode 10.2.1 was showing me the error
Implicit instantiation of undefined template 'std::__1::vector<std::__1::basic_string<char>, std::__1::allocator<std::__1::basic_string<char> > >'.
#include <iostream>
#include <vector>
int main(int argc, const char * argv[]) {
std::vector<std::string> listRestaurants;
....
return 0;
}
Fixed my issue.
If adding std:: is not the issue for you, then check if you have #include <vector>. That fixed the issue for me.
Didn't realize that #include <vector> is required. I thought it was part of standard library template; I ran this code in VSCODE IDE and it worked fine for me
#include <iostream>
#include <vector>
using namespace std;
int main()
{
uint_least8_t i; // trying to be conscious of the size of the int
vector<int> vect;
for(i = 0; i < 5; ++i)
{
vect.push_back(i);
}
for(auto i : vect)
{
cout << i << endl;
}
return 0;
}
From the comments:
The std namespace houses both of those templates. Change vector to std::vector, and string to std::string. – WhozCraig
the vector and string were placed in the namespace std
using namespace std;
This code works for me:
#include <string>
#include <iostream>
int main()
{
std::string s;
s = "hello world";
cout << s;
return 0;
}
But this one doesn't:
#include <string>
int main()
{
string s;
s = "hello world";
return 0;
}
Is the include of <iostream> needed as well as the <string> one?
I'm using Eclipse CDT IDE.
Iostream is not needed to use string. You are missing using namespace std (or alternatively using the std:: prefix) in the second example, that's why it's not working.
I have coded the following:
STDMETHODIMP CWrapper::openPort(LONG* m_OpenPortResult)
{
std::string str;
//const char * c = str.c_str();
// Open("test".c_str())
return S_OK;
}
The compiler tells me "There is no such member "string" in the namespace std".
My includes look like this:
#include "stdafx.h"
#include "Wrapper.h"
#include <string.h>
using namespace std;
Did I do anything wrong so far?
You need to add the following:
#include <string>