Does anyone know what header file is missing? - c++

This code works on Code Blocks but not on Visual Studio:
// A simple program that prints string test1:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<string> test1 = { "pooping","reading" };
for (vector<string>::iterator iter = test1.begin(); iter != test1.end(); iter++)
{
cout << *iter << endl;
}
system("pause");
}
The error that Visual Studio Outputs:
Error C2679 binary '<<': no operator found which takes a right-hand operand of type 'std::_Vector_iterator<std::_Vector_val<std::_Simple_types<std::basic_string<char,std::char_traits<char>,std::allocator<char>>>>>' (or there is no acceptable conversion)

std::basic_string is officially defined in the <string> header. See cppreference.

#include<string>
The class template basic_string stores and manipulates sequences of char-like objects. The class is dependent neither on the character type nor on the nature of operations on that type. The definitions of the operations are supplied via the Traits template parameter - a specialization of std::char_traits or a compatible traits class. For more details please see
std::basic_string

after adding header , program won't compile and it will throw error
"error: in C++98 'test1' must be initialized by constructor, not by '{...}'"
check the below program followed another method to initialize vector.
As suggested by other you need to add
#include<string>
// A simple program that prints string test1:
#include <iostream>
#include <vector>
#include<string>
using namespace std;
int main()
{
static const string arr[] = {"pooping","reading"};
vector<string> test1 (arr, arr + sizeof(arr) / sizeof(arr[0]) );
for (vector<string>::iterator iter = test1.begin(); iter != test1.end(); iter++)
{
cout << *iter << endl;
}
system("pause");
}

Related

C++ Using Visual Studio 2020 cant use .length()

I am sorry if this is really basic but I just started and am extremely confused. I am trying to find the length of a string s using .length() I have included #include , #include "genlib.h"and #include "simpio.h", but am still getting an error.
#include <iostream>
#include "genlib.h"
#include "simpio.h"
#define MAX_HASH_CODE 10000
int Hash( int maxCode, string s);
#define Multiplier -1664117991L // Multiplier used in Hash function
int Hash(int maxCode, string s)
{
unsigned long hashcode = 0;
for (int i = 0; i < s.length(); i++)
hashcode = hashcode * Multiplier + s[i];
return (hashcode % maxCode);
}
int main ()
{
std::cout << "Please enter your name: ";
string name = GetLine();
int hashcode = Hash(MAX_HASH_CODE, name);
std::cout << " The hash code for your name is " << hashcode << "." <<std::endl;
return 0;
}
s.length() just gives an error and says:
request for member 'length' in 's', which is of non-class type 'string' {aka 'char*'}gcc
image of error
By looking at your error message, it looks like you're using the Visual Code IDE, not Visual Studio.
The reason why you are experiencing the error is because it does not recognize s as a string because you have not defined it as such. Even though you declared s to be of type string, it is not the std string. The data type string is only available if you import the <string> library with #include <string>; however, in C++ all standard libraries are including inside a namespace, std. To access the functions like cout and string inside of the std just include using namespace std; at the top like an include statement. However, note that by doing this any methods you declare with the same name as a function inside the std namespace will create a name conflict. You can instead just call the function by the namespace prefix std::, to which you are using by std::cout.
To fix your problem either import the <string> library, include the std namespace, or every time you want to use a string, use std::string s to clarify that you want the std string.
Method 1 (Only works in C, not C++):
#include <string>
string s;
int length = s.length()
Method 2:
using namespace std;
string s;
int length = s.length()
Method 3:
std::string s;
int length = s.length()
Also, a similar question like this has been posted here:
Why am I getting string does not name a type Error?

c++ iterate through a vector of strings

So I recently discovered the use of map and vectors, however, I'm having trouble of trying to figure a way to loop through a vector containing strings.
Here's what I've tried:
#include <string>
#include <vector>
#include <stdio>
using namespace std;
void main() {
vector<string> data={"Hello World!","Goodbye World!"};
for (vector<string>::iterator t=data.begin(); t!=data.end(); ++t) {
cout<<*t<<endl;
}
}
and when I try to compile it, I get this error:
cd C:\Users\Jason\Desktop\EXB\Win32
wmake -f C:\Users\Jason\Desktop\EXB\Win32\exbint.mk -h -e
wpp386 ..\Source\exbint.cpp -i="C:\WATCOM/h;C:\WATCOM/h/nt" -w4 -e25 -zq -od -d2 -6r -bt=nt -fo=.obj -mf -xs -xr
..\Source\exbint.cpp(59): Error! E157: col(21) left expression must be integral
..\Source\exbint.cpp(59): Note! N717: col(21) left operand type is 'std::ostream watcall (lvalue)'
..\Source\exbint.cpp(59): Note! N718: col(21) right operand type is 'std::basic_string<char,std::char_traits<char>,std::allocator<char>> (lvalue)'
Error(E42): Last command making (C:\Users\Jason\Desktop\EXB\Win32\exbint.obj) returned a bad status
Error(E02): Make execution terminated
Execution complete
I tried the same method using map and it worked. The only difference was I changed the cout line to:
cout<<t->first<<" => "<<t->last<<endl;
Add iostream header file and change stdio to cstdio.
#include <iostream>
#include <string>
#include <vector>
#include <cstdio>
using namespace std;
int main()
{
vector<string> data={"Hello World!","Goodbye World!"};
for (vector<string>::iterator t=data.begin(); t!=data.end(); ++t)
{
cout<<*t<<endl;
}
return 0;
}
#include <iostream>
#include <vector>
#include <string>
int main()
{
std::vector<std::string> data = {"Hello World!", "Goodbye World!"};
for (std::vector<std::string>::iterator t = data.begin(); t != data.end(); t++) {
std::cout << *t << std::endl;
}
return 0;
}
Or with C++11 (or higher):
#include <iostream>
#include <vector>
#include <string>
typedef std::vector<std::string> STRVEC;
int main()
{
STRVEC data = {"Hello World!", "Goodbye World!"};
for (auto &s: data) {
std::cout << s << std::endl;
}
return 0;
}
From the Open Watcom V2 Fork-Wiki on the C++ Library Status page:
<string>
Mostly complete. Although there are no I/O operators, all other member functions and string operations are available.
A workaround (besides implementing the << operator) would be asking the string instances for the C string:
for (vector<string>::iterator t = data.begin(); t != data.end(); ++t) {
cout << t->c_str() << endl;
}
This of course only works as long as the strings don't contain zero byte values.
When I compile your code, I get:
40234801.cpp:3:17: fatal error: stdio: No such file or directory
#include <stdio>
^
You clearly have a header called "stdio" in your include path that you haven't shown us.
If you change that line to the standard #include <iostream>, then the only reported error is that you wrote void main() instead of int main(). Fix that, and it will build and run.
In passing, note also that using namespace should be avoided.
I found a solution to my own issue. Instead of using a c_str, I used std::string and switched to using the G++ compiler instead of Open Watcom
Instead of having:
char *someString="Blah blah blah";
I instead replaced it with:
string someString="Blah blah blah";
This way is much more efficient and easier.

C++ String - Out of scope error

I was trying to play around with Strings in a Hangman program that I'm writing and couldn't get them to work so tried working with them on a simpler basis and I'm still having no luck.
As far as I've read online in the references and what other people have said this code should work:
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
int main (int argc, char** argv){
string word = {"Hello"};
int length = strlen(word);
}
But I get this compiler error:
'string' was not declared in this scope
and consequently, 'word' is also not declared in scope.
Can anyone see what I'm doing wrong? I'm using the g++ compiler on Ubuntu if that makes a difference, no idea which version though.
You are confusing C and C++.
You included only C libraries, whereas std::string comes from the C++ header string. You'd have to write:
#include <string>
to use it. However, you'd then have to make other changes, such as not using strlen.
You should learn from your C++ book, not random posts on the internet (#lolirony)
C version
#include <string.h>
int main(void)
{
const char* word = "Hello";
const size_t length = strlen(word); // `size_t` is more appropriate than `int`
return 0;
}
C-like C++ version
#include <cstring>
using namespace std;
int main()
{
const char* word = "Hello";
const size_t length = strlen(word);
}
Idiomatic C++ version (recommended)
#include <string>
int main()
{
const std::string word = "Hello";
const std::size_t length = word.size();
}
'string' was not declared in this scope
You need to include the header <string> and refer to it as std::string. Also, strlen does not understand std::string or any user defined types, but you can use the size() method instead:
#include <string>
int main()
{
std::string word = "Hello";
size_t length = word.size();
}
<cstring> is the header for C++ support of C-style null-terminated strings. You should include <string>.
You haven't included the C++ string header in your project.
#include <string>
The libraries that you've included are all plain-C headers.
Additionally, strlen() doesn't work with a c++ string; you should use word.size() instead.
string is a specialization of standard class std::basic_string . It is declared in header <string>
So if you want "to play around with standard class std::string:" you need to include directive
#include <string>
Header <cstring> is not the same as header <string> and contains declarations of standard C functions such as strlen.
However there is no any sense to apply function strlen to an object of type std::string The compiler in this case will issue an error.
I advice you to play with the following code that to see the difference
#include <iostream>
#include <string>
#include <cstring>
int main (int argc, char** argv)
{
std::string word = "Hello";
std::string::size_type length = word.length();
std::cout << "Object word of type std::string has value "
<< word << " with length of " << length
<< std::endl;
std::cout << "The size of the object itself is " << sizeof( word ) << std::endl;
char another_word[] = "Hello";
size_t another_length = std::strlen( another_word );
std::cout << "Object another_word of type char [6] has value "
<< another_word << " with length of " << another_length
<< std::endl;
std::cout << "The size of the object itself is " << sizeof( another_word ) << std::endl;
}

Need assistancce with c++ sytem() functionn

ok so i wrote a program which scaans all directories an sub directories to find sepcific file extensions(i pass a string for the extension type i want) then it returns a vector loade with all the filenames with the specific filetype extensions. then i have another class which has a function that print out all of the files in the vector and will then will iterate the vector and run a program in that vector that the user chooses. This is my problem. Getting the file from the vvector to run. i am using visual studios on windows 7
using boost filesystem V3. this is my current function:
#define BOOST_FILESYSTEM_NO_DEPRECATED
#ifndef NotePad__h
#define NotePad__h
#include boost/filesystem.hpp
#include iostream
#include io.h
#include stdlib.h
#include stdio.h
#include cstdlib
#include Windows.h
#include atlstr.h
#include string
#include cstring
;
namespace fs = boost::filesystem;
class NpLaunch
{
public:
void Launch (const std::vector<fs::path>& v)
{
int count=0;
std::cout << "launched in notePad.h" << std::endl;
for(auto i = v.begin(); i!= v.end(); ++i)
{
//string s;
//string val = (string) itr;
std::cout << count << ". " << *i << std::endl;
++count;
std::string s = i->c_str();
//std::system(i->c_str());
}
}
};
#endif
and this is the error im getting:
Error 1 error C2440: 'initializing' : cannot convert from 'const >boost::filesystem3::path::value_type *' to 'std::basic_string<_Elem,_Traits,_Ax>' >c:\users\admin\documents\visual studio 2010\projects\launcher\launcher\notepad.h 31
On Windows, path::value_type is a wchar_t, and thus path::string_type is equivilent to std::wstring, and the path::c_str() method returns a wchar_t*. You cannot assign a wchar_t* to a std::string, that is what the compiler error is trying to tell you.
To assign a path object to a std::string, you have to perform a character conversion from wchar_t to char. The path::string() method does that for you, eg:
std::string s = i->string();
Otherwise, use a std::wstring instead, which you can assign to using either the path::native() or path::wstring() method, eg:
std::wstring s = i->native();
std::wstring s = i->wstring();

Using boost phoenix, how can I invoke a find_if call with starts_with?

I'm trying to find an element in a vector of structs. The code works when searching in a case-sensitive manner. When I try enhancing it to be case-insensitive, I run into two issues.
Simply including boost/algorithm/string.hpp breaks the previously working VS2010 build. The error is "'boost::phoenix::bind' : ambiguous call to overloaded function". Builds OK in Xcode. Any way to disambiguate the bind?
I guess I've got the syntax wrong in the second (commented out) find_if line, adding the istarts_with call. I get errors from the phoenix headers saying "error: no type named 'type'". Assuming issue #1 can be fixed, how should I correct this line?
Thanks!
Code:
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <boost/algorithm/string.hpp> // This include breaks VS2010!
#include <boost/phoenix/bind.hpp>
#include <boost/phoenix/core.hpp>
#include <boost/phoenix/operator.hpp>
#include <boost/phoenix/stl/algorithm.hpp>
using namespace boost::phoenix;
using boost::phoenix::arg_names::arg1;
using boost::istarts_with;
using std::string;
using std::cout;
// Some simple struct I'll build a vector out of
struct Person
{
string FirstName;
string LastName;
Person(string const& f, string const& l) : FirstName(f), LastName(l) {}
};
int main()
{
// Vector to search
std::vector<Person> people;
std::vector<Person>::iterator dude;
// Test data
people.push_back(Person("Fred", "Smith"));
// Works!
dude = std::find_if(people.begin(), people.end(), bind(&Person::FirstName, arg1) == "Fred");
// Won't build - how can I do this case-insensitively?
//dude = std::find_if(people.begin(), people.end(), istarts_with(bind(&Person::FirstName, arg1), "Fred"));
if (dude != people.end())
cout << dude->LastName;
else
cout << "Not found";
return 0;
}
You'd need two binds to make it work. First define:
int istw(string a, string b) { return istarts_with(a,b); }
and then use the following as the predicate for the find_if:
bind(&istw,bind(&Person::FirstName, arg1),"fred")
Two comments:
Make sure you're using the right bind, namely, use boost::phoenix::bind.
The definition of istw is probably unnecessary but I could not find the right way to replace it.