I am stuck for hours during my assignment. Specifically, on this part:
The constructor should take a const-qualified C-Style string as its argument. Use the strncpy() function from the <cstring> library to copy it into the underlying storage. Be sure to manually null-terminate the attribute after you copy to assure that it is a valid C-String (in case the parameter contained a much larger string).
Where am I making mistakes, and how should I change my code?
#ifndef STRINGWRAPPER_H
#define STRINGWRAPPER_H
class StringWrapper{
public:
StringWrapper (const char myString);
const static int max_capacity = 262144;
private:
int size = 1;
char myString [40];
};
#endif
#include "StringWrapper.h"
#include <cstring>
StringWrapper::StringWrapper (const char myString){
strncpy(StringWrapper::myString, myString, sizeof(myString));
}
#include <iostream>
#include "ThinArrayWrapper.h"
#include "ArrayWrapper.h"
#include "StringWrapper.h"
#include <stdexcept>
int main(){
char myString[]{ "string" };
StringWrapper StringWrapper('h');
return 0;
}
First of all, your call to strncpy is wrong. Please check the reference regarding the strncpy from here.
According to the definition of strncpy :
char *strncpy(char *dest, const char *src, std::size_t count);
In your case, you are calling strncpy like this:
strncpy(StringWrapper::myString, myString, sizeof(myString));
Here, myString is a const char type variable. You need to make it to const char *. If you like, you can check my modification of your code from here.
Related
This question already has answers here:
Conversion from string literal to char* is deprecated [duplicate]
(2 answers)
Closed 2 years ago.
So I'm building a Regex class, with a simple constructor:
regex.hpp
#ifndef REGEX_CUSTOM_CLASS
#define REGEX_CUSTOM_CLASS
#include <stdio.h>
using namespace std;
class Regex
{
private:
/* data */
public:
char *regex;
Regex(char str[]);
};
#endif // REGEX_CUSTOM_CLASS
regex.cpp
#include <iostream>
#include <list>
#include <assert.h>
#include <ctype.h>
#include <stdio.h>
#include "regex.hpp"
using namespace std;
Regex::Regex(char str[])
{
regex = str;
}
tools.cpp
#include <iostream>
#include <stdio.h>
#include "lib/regex/regex.cpp"
using namespace std;
int main() {
Regex my_regex("//");
cout << my_regex.regex << endl;
return 0;
}
But after I compile it to .exe file and run it, I get this error message:
warning: ISO C++ forbids converting a string constant to 'char*' Regex my_regex("//");
I think the problem is with the data types. What is the problem?
You cannot pass arrays by value. When you write:
Regex::Regex(char str[])
this actually is
Regex::Regex(char* str)
Moreover string literals are of type const char [N] (where N is length of the string including the null terminator) and when passed to functions they decay to const char*. Getting a char* from a const char* (pointer to constant char, not to be confused with constant pointer to char) would break const correctness.
Either use std::string as argument type or change it to const char*.
This question already has answers here:
How to convert a std::string to const char* or char*
(11 answers)
Closed 3 years ago.
For an application that I'm writing, I have a string type variable that I want to display within an ncurses window:
#include <iostream>
#include <ncurses.h>
#include <string>
int main(){
std::string mystring = "A sample string\n";
// Entering the ncurses window
initscr();
printw(mystring);
getch();
endwin();
}
which throws the following error at compilation:
test_app.cpp: In function ‘int main()’:
test_app.cpp:12:18: error: cannot convert ‘std::__cxx11::string {aka std::__cxx11::basic_string<char>}’ to ‘const char*’ for argument ‘1’ to ‘int printw(const char*, ...)’
printw(mystring);
Where am I going wrong? How can I rectify this?
Some key concepts in c++:
A string literal declaration (aka "this is a string literal") has a type const char[N], where N is the size of the string, including the null terminator.
std::string != const char[]
However, a std::string can be constructed with a const char[] using this constructor (found here):
basic_string( const CharT* s,
const Allocator& alloc = Allocator() );
Where CharT is your implementation specific char equivalent.
Now, notice how printw takes a const char*. You aren't passing a const char * to printw, you're passing a std::string, and they aren't implicitly convertible to a const char *.
We have two options to solve your problem...
1) Store the string as a char[] (aka char *):
#include <iostream>
#include <ncurses.h>
#include <string>
int main(){
char mystring[] = "A sample string\n"; // Can decay to a char * implicitly.
// Entering the ncurses window
initscr();
printw(mystring);
getch();
endwin();
}
2) Get a representation of the std::string as a char *:
#include <iostream>
#include <ncurses.h>
#include <string>
int main(){
std::string mystring = "A sample string\n";
// Entering the ncurses window
initscr();
// Since c++ 11, mystring.data() is required to return a null-terminated char *.
// If c++ version < c++11, use mystring.c_str().
printw(mystring.data());
getch();
endwin();
}
I am working with C++ in eclipse CDT and I am trying to convert string to uint64_t by using strtoull but everytime I get below error message -
..\src\HelloTest.cpp:39:42: error: strtoull was not declared in this scope
Below is my C++ example
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
int main() {
string str = "1234567";
uint64_t hashing = strtoull(str, 0, 0);
cout << hashing << endl;
}
return 0;
}
Is there anything wrong I am doing?
Why your solution doesn't work has already been pointed out by others. But there hasn't been a good alternative suggested yet.
Try this for C++03 strtoull usage instead:
#include <string>
#include <cstdlib>
int main()
{
std::string str = "1234";
// Using NULL for second parameter makes the call easier,
// but reduces your chances to recover from error. Check
// the docs for details.
unsigned long long ul = std::strtoull( str.c_str(), NULL, 0 );
}
Or, since C++11, do it directly from std::string via stoull (which is just a wrapper for the above, but saves on one include and one function call in your code):
#include <string>
int main()
{
std::string str = "1234";
// See comment above.
unsigned long long ul = std::stoull( str, nullptr, 0 );
}
Never use char[] or pointers if you have a working alternative. The dark side of C++, they are. Quicker, easier, more seductive. If once you start down the dark path, forever will it dominate your destiny, consume you it will. ;-)
the structure for strtoull is: strtoull(const char *, char * *, int)
You have given it a std::string as pointed out by #juanchopanza
This is the solution I came up with is
#include <iostream>
#include <cstring>
#include <string>
#include <cstdlib>
using namespace std;
int main() {
char str[] = "1234567";
unsigned long long ul;
char* new_pos;
charDoublePointer = 0;
ul = strtoull(str, &new_pos, 0);
cout << ul << endl;
return 0;
}
The output I got was: 1234567
Straight from the eclipse console.
Also at the end of your program you have return 0 out of scope with an extra curly brace.
I have been working on a program to test out a few string manipulation possibilities. It is basically supposed to read a string list and be able to find a character's neighbors to go through the strings as a circuit. Here's the code:
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
std::string grid[20]={" "};
std::string get(int string, int member){
return grid[string].substr(member,1);
}
std::string* getNeighbors(int string, int member){
std::string neighbors[4];
neighbors[0]=grid[string-1].substr(member,1);//up
neighbors[1]=grid[string+1].substr(member,1);//down
neighbors[2]=grid[string].substr(member-1,1);//left
neighbors[3]=grid[string].substr(member+1,1);//right
std::string* p=neighbors;
return p;//Returns up,down,left,right.
}
int main(int argc, char** argv){
grid[1]="#----^---0";
grid[2]="abcdefghi0";
grid[3]="jklmnopqr0";//TODO Change to read of txt*/
std::string* neighbors;
for(int i=0;grid[1].length()>i;i++){
neighbors=getNeighbors(2,1);
if(neighbors[3]=="-" | neighbors[3]=="^"){
std::string r=get(1,i);
(r!="0") ? std::cout<<r:0;//Dangerous. TODO Unknown symbol handling
std::cout<<neighbors[3];
}
}
}
This compiles well, but has the runtime error "Segmentation fault: 11". I am using several subjects and techniques that I am not used to and am likely misusing. Any help would be great.
std::string neighbors[4]; is stack allocated. When you go out getNeighborsit looses scope. Try to put it other place (even globaly, just as a proof of concept). A better design should be pass this as parater to your function.
void getNeighbors(int string, int member, std::vector<std::string>& neighbors){
;
neighbors[0]=grid[string-1].substr(member,1);//up
neighbors[1]=grid[string+1].substr(member,1);//down
neighbors[2]=grid[string].substr(member-1,1);//left
neighbors[3]=grid[string].substr(member+1,1);//right
}
EDIT:
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
std::string grid[20]={" "};
std::string neighbors[4]; //<---------------------------
std::string get(int string, int member){
return grid[string].substr(member,1);
}
std::string* getNeighbors(int string, int member){
neighbors[0]=grid[string-1].substr(member,1);//up
neighbors[1]=grid[string+1].substr(member,1);//down
neighbors[2]=grid[string].substr(member-1,1);//left
neighbors[3]=grid[string].substr(member+1,1);//right
std::string* p=neighbors;
return p;//Returns up,down,left,right.
}
int main(int argc, char** argv){
grid[1]="#----^---0";
grid[2]="abcdefghi0";
grid[3]="jklmnopqr0";//TODO Change to read of txt*/
std::string* neighbors;
for(int i=0;grid[1].length()>i;i++){
neighbors=getNeighbors(2,1);
if(neighbors[3]=="-" | neighbors[3]=="^"){
std::string r=get(1,i);
(r!="0") ? std::cout<<r:"0";//Dangerous. TODO Unknown symbol handling
std::cout<<neighbors[3];
}
}
}
The neighborsnow is global (I don´t like this, but do the job for the POC).
getNeighbors() is returning a pointer to a local variable.
I believe this is the right header:
#include <cstdio>
Note, there is a difference between the above declaration and this one:
#include <stdio.h>
The first one puts everything in the "std" namespace, the 2nd one doesn't. So I am using the first one.
Below is the code which I am compiling using g++4.4.6 on aix6.1:-
#include <cstdarg> //< va_list
#include <cstdio> //< vsnprintf()
#include "virtual_utils.h"
namespace VS
{
const char* format_str( const char* str, ... ) throw()
{
static char buf[10][1024];
static unsigned long buf_no = 0;
char* cur_buf = buf[ ++buf_no % 10 ];
buf_no %= 10;
va_list vl;
va_start( vl, str );
#ifdef _MSC_VER
std::_vsnprintf( cur_buf, sizeof(buf), str, vl );
#else
std::vsnprintf( cur_buf, sizeof(buf), str, vl );
#endif
return cur_buf;
}
} //< namespace VS
These are the following errors which I am getting:-
virtual_utils.C: In function 'const char* VS::format_str(const char*, ...)':
virtual_utils.C:28: error: 'vsnprintf' is not a member of 'std'
Edit:
Modifying the above code to remove the #include "virtual_utils.h" and to add a main(), it compiles with a warning under gcc4.3.4 on Ideone and cleanly under gcc4.5.1.
Compile with --save-temps, and examine the .ii file it produces. That should make it clear what's defined in what namespace, and what isn't.