0

My code is as follows.

#include "test.h"
#include "string"
#include "iostream"
using namespace std::string::find;


test::test(){
    string str ("ffs test ffs");
    string str2 ("test");
    if (str.find(str2) != std::string::npos) {
        std::cout << "found" << "\n";
    } else {
        std::cout << "not found" << "\n";
    }
}

the issue I'm having is this, when trying to define a string in the C++ file qt states "unknown type name 'string'". Also on line 4 my 'import' highlights string as if it doesn't exist, despite it being an option the editor suggests to me while I'm typing it. What am I doing wrong here? Everything I find is to try and fix issues passing stuff to QStrings and nothing related to my issue as far as I can tell. I've tried both types of importing #include <thing> and #include "thing" on all the imports it doesn't seem to make a difference.

Cœur
  • 37,241
  • 25
  • 195
  • 267
problemlow
  • 61
  • 1
  • 11

3 Answers3

4

Use std::string instead of string.

#include "test.h"
#include <string>
#include <iostream>

test::test(){
    std::string str ("ffs test ffs");
    std::string str2 ("test");
    if (str.find(str2) != std::string::npos) {
        std::cout << "found" << "\n";
    } else {
        str::cout << "not found" << "\n";
    }
}

Don't use using namespace (of course in your case, it wasn't a namespace, so that's another error), use <> for system headers.

Matthieu Brucher
  • 21,634
  • 7
  • 38
  • 62
2

After inclusion of the appropriate headers iostream, string etc, you can write:

using std::string;

This will bring in only string from the namespace std into your program. And you can do this if you want to avoid typing std::string everywhere. You can do this for stream objects like cout, cin as well.

using std::cout;
using std::cin;
P.W
  • 26,289
  • 6
  • 39
  • 76
1

Use Scope operator :: in Your Code and Access manually to std class

std::string

it will help you !