正在尝试打印向量的内容 我一直试图让C++变得越来越舒服,我已经尝试写一些文件MIIP的东西。我正在做一些能够解析fasta文件的事情,但我有点卡住了: #include<fstream> #include<iostream> #include<string> #include<vector> using namespace std; //A function for reading in DNA files in FASTA format. void fastaRead(string file) { ifstream inputFile; inputFile.open(file); if (inputFile.is_open()) { vector<string> seqNames; vector<string> sequences; string currentSeq; string line; while (getline(inputFile, line)) { if (line[0] == '>') { seqNames.push_back(line); } } } for( int i = 0; i < seqNames.size(); i++){ cout << seqNames[i] << endl; } inputFile.close(); } int main() { string fileName; cout << "Enter the filename and path of the fasta file" << endl; getline(cin, fileName); cout << "The file name specified was: " << fileName << endl; fastaRead(fileName); return 0; }

正在尝试打印向量的内容 我一直试图让C++变得越来越舒服,我已经尝试写一些文件MIIP的东西。我正在做一些能够解析fasta文件的事情,但我有点卡住了: #include<fstream> #include<iostream> #include<string> #include<vector> using namespace std; //A function for reading in DNA files in FASTA format. void fastaRead(string file) { ifstream inputFile; inputFile.open(file); if (inputFile.is_open()) { vector<string> seqNames; vector<string> sequences; string currentSeq; string line; while (getline(inputFile, line)) { if (line[0] == '>') { seqNames.push_back(line); } } } for( int i = 0; i < seqNames.size(); i++){ cout << seqNames[i] << endl; } inputFile.close(); } int main() { string fileName; cout << "Enter the filename and path of the fasta file" << endl; getline(cin, fileName); cout << "The file name specified was: " << fileName << endl; fastaRead(fileName); return 0; },c++,string,vector,ifstream,C++,String,Vector,Ifstream,并识别以“>”开头的内容,将其推送到vector seqNames上,然后将内容报告给命令行。-因此,我正在尝试编写检测快速格式头的功能。 然而,当我编译时,我被告知: n95753:Desktop wardb$ g++ testfasta.cpp testfasta.cpp:25:25: error: use of undeclared identifier 'seqNames' for( int i = 0; i < seqNames.size(); i++){

并识别以“>”开头的内容,将其推送到vector seqNames上,然后将内容报告给命令行。-因此,我正在尝试编写检测快速格式头的功能。 然而,当我编译时,我被告知:

n95753:Desktop wardb$ g++ testfasta.cpp
testfasta.cpp:25:25: error: use of undeclared identifier 'seqNames'
    for( int i = 0; i < seqNames.size(); i++){
                        ^
testfasta.cpp:26:17: error: use of undeclared identifier 'seqNames'
        cout << seqNames[i] << endl;
n95753:Desktop wardb$g++testfasta.cpp
testfasta.cpp:25:25:错误:使用未声明的标识符“seqNames”
对于(int i=0;icout这是因为您在
if
的内部范围内声明了向量。您需要将声明移出,以便
while
循环也可以看到它们:

vector<string> seqNames;
vector<string> sequences;
if (inputFile.is_open()) {
    string currentSeq;
    string line;
    while (getline(inputFile, line))
    {
        if (line[0] == '>') {
            seqNames.push_back(line);
        }
    }
}
for( int i = 0; i < seqNames.size(); i++){
    cout << seqNames[i] << endl;
}
矢量序列名称;
向量序列;
if(inputFile.is_open()){
字符串currentSeq;
弦线;
while(getline(inputFile,line))
{
如果(第[0]行=='>'){
seqNames.向后推(行);
}
}
}
对于(int i=0;iif(inputFile.is_open()){
矢量名称;
向量序列;
...
}
对于(int i=0;i我可以建议您使用一些IDE吗,这样您甚至在编译之前就可以发现这些琐碎的错误。(我不是反对者)我使用Xcode作为IDE,尽管只使用一个cpp文件,但由于某种原因,在Xcode中打开整个项目是不一样的——仍然习惯使用它。
vector<string> seqNames;
vector<string> sequences;
if (inputFile.is_open()) {
    string currentSeq;
    string line;
    while (getline(inputFile, line))
    {
        if (line[0] == '>') {
            seqNames.push_back(line);
        }
    }
}
for( int i = 0; i < seqNames.size(); i++){
    cout << seqNames[i] << endl;
}
if (inputFile.is_open()) {
    vector<string> seqNames;
    vector<string> sequences;
    ...
}
for( int i = 0; i < seqNames.size(); i++){
    cout << seqNames[i] << endl;
}