std::string depreciated ?

i have three files: 1: main 2: math functions 3: header
when i compile (g++ Linux Mint Rebecca) them I get the following error: 'string' is not a member of 'std' \ int getPosInt(std::string);

i assume that means that the string function is not in the standard (std) library? if true where is it?

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

using namespace std;

int main(){
    int radius = getPosInt("Enter a positive integer for the radius of a circle: ");
    double aCircle = areaOfCircle(radius);
    double vSphere = volOfSphere(radius);

    cout << "The area of a circle with radius of " << radius << " is: " <<  aCircle << endl;
    cout << "The area of a sphere with the radius of " << radius << " is: " << vSphere << endl;    

    return 0;
}

functions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <cmath>
#include "myMathFunctions.h"
using namespace std;

// - get a positive integer value from the user and return that value
int getPosInt(string msg){
    int num = 0;
    do{
        cout << msg;
        cin >> num;
    }while(num <= 0);
    return num;
}

// - calculate the area of a circle based on a radius.
// - (Area) A = π r2
double areaOfCircle(int r){
    return (PI * pow(r,2));
}


header
1
2
3
4
5
6
7
8
9
10
#ifndef MYMATHFUNCTIONS_H_
#define MYMATHFUNCTIONS_H_

const double PI = 3.14159;

int getPosInt(std::string);
double areaOfCircle(int);
double volOfSphere(int);

#endif 
also, if i put this into one source file and remove 'std::' from the getPosInt function it compiles fine.
If you're using std::string, you need to #include <string>
The reason it worked for if you put it all in one file is that <iostream> does define string. However, it doesn't necessarily define any of the functions used with strings, so its always better to just include <string> when dealing with that type.
im confused on how these files pass ?_? am i to use #include <iostream> on every source file that uses in/output stream? i thought that since i declared it in main its declared through out the rest of the program?.

also, is there a certain order files should be sent to the compiler? Does the main function file have to come before rest? i compile this in terminal:
g++ project.cpp myMathFunctions.cpp myMathfunctions.h -o project

thanks for the help.
am i to use #include <iostream> on every source file that uses in/output stream?
Yes. Each compilation unit (source file) is compiled independendtly from others. So you must include all headers your header/implementatin file uses. If you use std::string, include <string>. If you use cin, include <iostream>, etc.

is there a certain order files should be sent to the compiler?
For compiling, no. For linking, some compilers, gcc for example, does depend on library order.
thanks MiiNiPaa!
two more questions , should the header file also include headers used in it? for example, in my header file i should include #include <string>

second question, does #include <iostream> contain the string definition in it? im guessing it does because i normally dont include the string header but am still able to declare variables as strings.
Last edited on
should the header file also include headers used in it?
Yes. But it is a good idea to think if it is really needed. Forward declaration might suffice if only pointer/reference to class is passed as parameter/declared as member.

does #include <iostream> contain the string definition in it?
No. It does not. Standard does not list std::string as something contained in <iostream>. However some internals might include parts of the <string> header. But it is not required and you should not rely on that behavior. At least one alternative STL implementation does includes basic_string definition, but does not define std::string/wstring/etc. type aliases in <iostream>. libstdc++ AFAIR defines type aliases, but does not include any non_member functions like std::to_string

In short you should always include headers containing something you use directly and do not rely on undocumented and nonmandatory behavior.
thanks for you explanations and time. i appreciated it.
Topic archived. No new replies allowed.