简体   繁体   中英

C++:declaring a function that returns string in header file?

In one of my modules, I have a function (changeNum) that returns a string and accepts a parameter that is a string. I tried to declare this function in my header file as following:

std::string changeNum(std::string s); 

[and I included the string header file into the header file as well]

but I'm still getting the following error in my header file: "unknown type name 'string'" What do I do?

Here is the whole code: My header file is the following:

#pragma once
#include <string>
std::string changeNum(std::string s); 

My module with the function changeNum is defined as the following

#include <string>
string changeNum(string s){
    return s;
}

Try it:

Header.h

#pragma once
#include <string>

std::string changeNum(std::string s);

Source.cpp

#include "Header.h"

std::string changeNum(std::string s) {
    return s;
}

main.cpp

#include "Header.h"
#include <iostream>

int main()
{
    std::string sample_str = changeNum("Hello");
    std::cout << sample_str.c_str();
}

Tested on VS and removed #include "pch.h" from aforementioned code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM