![](/img/trans.png)
[英]Why can't I define a count variable outside of my loop in this function (C++)
[英]I can't define a string in my C++ code in qt
我的代碼如下。
#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";
}
}
我遇到的問題是,當嘗試在 C++ 文件中定義字符串時,qt 指出“未知類型名稱‘字符串’”。 同樣在第 4 行,我的“導入”突出顯示了字符串,就好像它不存在一樣,盡管它是編輯器在我鍵入時向我建議的一個選項。 我在這里做錯了什么? 我發現的一切都是嘗試解決將東西傳遞給 QStrings 的問題,據我所知,與我的問題無關。 我已經嘗試了兩種類型的導入#include <thing>
和#include "thing"
對所有導入它似乎沒有什么區別。
使用std::string
而不是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";
}
}
不要使用using namespace
(當然在你的情況下,它不是命名空間,所以這是另一個錯誤),使用 <> 作為系統標頭。
在包含適當的標頭iostream
、 string
等之后,您可以編寫:
using std::string;
這只會將namespace std
string
引入您的程序。 如果您想避免在任何地方鍵入std::string
,您可以這樣做。 您也可以對cout, cin
等流對象執行此操作。
using std::cout;
using std::cin;
在您的代碼中使用 Scope 運算符 :: 並手動訪問 std 類
std::string
它會幫助你!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.