string Library

When I use the library
#include<cstring> in my
C++ program I do not have
any problems with 'string'
declarations. I am only using the
'using namespace std; '
-----------

However, when I use #include<string>
I have errors. I have to use both
'using namespace std;'

and the std::string my_array[];
only then does it accept my string
declarations.
[ The Error it gives is "string does not name a type' ]
--------------------------

Is there a reason for this, or is this maybe just a CodeBlocks issue???
You need
#include <string>
not
#include <cstring>

Beyond that, show (all) your code.
Last edited on
cstring is for the c-style string functions, string is for the C++ std::string class.

If you use std::string, then you need:

 
#include <string> 

Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include<iostream>
#include<string>
using namespace std;
/***********************************
            ReadMe
    **********************
Sept 9, 2021 :

Using the book 'The C++ WorkShop by Packt>"
I am working on Pointers and Strings. Both
I never really got too far into - so I am
using this book to cover these topics better.
I think, I will stop using enums when I can
just design my own Look-Up Arrays, and they
can display actual words - - Enums cannot.
                ********
Remember : Once we set a pointer all of the
            calculations are from that point.
            p+4 = starting point plus four
            A pointer can be reset to another
            address unless you're using a
            const pointer - a whole different
            subject (not covered here).
                ************/


int main(){
string Weapons[6] {"Revolver","Mace","4ft Spear","Club 5lb","Shiraka","Magic Mushroom"};

            /***********/
string* W_ptr=nullptr;
W_ptr=&Weapons[0];


std::cout<<std::endl<<std::endl;
std::cout<<"You now have a "<<*(W_ptr+2)<<std::endl<<std::endl;
// To Determine The Size of The Weapons Array Without Actually Knowing its Size
cout<<"The Weapons Array is a Size Of : "<<(sizeof Weapons/sizeof Weapons[0]);
cout<<std::endl<<std::endl<<std::endl;


return 0;}   



My program includes notes to self -- you can ignore all that chatter .... LoL


====================================================================

When I use the #include<string> Library and just the 'using namespace std '
I still have to include the std::string Array_Name - - - Not Just The ' string Array_Name'



I just thought that was the whole point of the ' using namespace std' so I did not have to type that every time. I'm not sure if there is just something I am doing wrong concerning this ...


Now on the other hand, if I use #include<cstring> and the 'using namespace std' I do not have to include the std::string XXX I can just type string XXX, like I expected.

I am not sure what the difference from C++ string to C string is....


Yea, I am working with pointers and strings ...
Last edited on
Your code compiles OK - I'm not sure what your question is about. You can try it in cpp.sh. You don't use std::string anywhere.

There is nothing in this code that uses <cstring>

I can't see the application of sizeof in line 38 working in general.
then it must be something in my IDE, it give the error
" String does not name a type'
if I use just the String library and using namespace std....


I think, I'll just deal with it then ...


thanks anyhow ...

I always used that application of 'sizeof' to find my array size without actually having to know it - rarely do I print it out mostly to pass to a function or something of that nature.
Last edited on
I just thought that was the whole point of the ' using namespace std' so I did not have to type that every time. I'm not sure if there is just something I am doing wrong concerning this ...


It's best practise to actually put std:: before each std thing. The whole point of namespaces is to keep identifiers separate form each other. When one has using namespace std , it brings in heaps of things from the STL, increasing the chance of clashes with variable or function identifiers. Here is a list of the current std symbols that come into scope when one has sing namespace std (providing the appropriate header file is included): https://en.cppreference.com/w/cpp/symbol_index . Who knows what identifiers may be added in the future, breaking your code.
For example there is a std::left in the iostream header, if one used left as a variable name, that causes a clash which can hard to spot.

One does get used to the extra typing. The other thing to do, is setup the IDE to do code snippets: have mnemonics like str say, once you type that, the editor replaces it with std::string

Putting std:: is explicit : std::copy is what it is, not boost::copy or some other copy from somewhere.

It's a good idea to put your own code into it's own namespace.

" String does not name a type'


Are you sure you didn't type String instead of string ? If String appears in the error message you must have.
A bit of reading why using namespace std; is not a good idea:
https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice

There are other sites that reiterate the idea, a meta link:
https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice

IMO it is just plain lazy and sloppy. I refuse to use it. I'd prefer to type a couple extra characters to make sure I don't accidentally pollute the global namespace and create potential name conflicts.

I'd also not use a regular array to hold data, I'd use a C++ container. First choice would be a std::vector. To pass a regular array as a function parameter requires also passing the array's size. A vector maintains its size internally as part of the container.
@OP
A C-style string is a const linear array of characters, hence const char*
<cstring> is only needed if you want to include functionality associated with C-style strings - not being used here so unnecessary to #include.
All of this means as follows, but highlights why C++ adopts the <string> class.

Also, using namespace std, covered up a couple of type 'sins' in the original program which obscured the type mismatching.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<iostream>

int main()
{
    const char* Weapons[]
    {"Revolver","Mace","4ft Spear","Club 5lb","Shiraka","Magic Mushroom"};
    
    const char** W_ptr =  &Weapons[0];
    
    std::cout
    << "You now have a "<< *(W_ptr+2) << '\n'
    << "There are " <<(sizeof Weapons/sizeof Weapons[0]) << " weapons\n";
    
    return 0;
}



You now have a 4ft Spear
There are 6 weapons
Program ended with exit code: 0
Topic archived. No new replies allowed.