C++ 不在构造函数定义中命名类型

标签 c++ constructor compilation

我尝试编译我的代码,我很确定我在标题或编译中犯了错误,但我不明白在哪里。我知道这是一个基本问题,我读了一些其他主题,但我真的不明白。我看了我写的其他一些代码,但没有发现任何区别......

g++ -c main.cpp -o out

我不明白这个错误,所以我也尝试:

g++ -c readFastqFile.cpp

错误

readFastqFile.cpp:8:1: error: ‘readFastq’ does not name a type
 readFastq::readFastq(){ //Constructor

我的文件是:

ma​​in.cpp

    #include <iostream>
    #include <string>

    #include "readFastqFile.hpp"

using namespace std;

int main(int argc, char *argv[]){
    cout << "hello" <<endl;
    //readFastq allReads;
    return 0;
}

readFastqFile.hpp

#ifdef READFASTQFILE_HPP
#define READFASTQFILE_HPP

#include <iostream>
#include <string>

using namespace std;

class readFastq{
    public:
        readFastq(); //constructor

    private:
        string readName;
        string sequence;
        string score;
};
#endif // READFASTQFILE_HPP

readFastqFile.cpp

 #include <string>
    #include <iostream>
#include "readFastqFile.hpp"

using namespace std;

readFastq::readFastq(){ //Constructor
    readName = "bla";
    cout << readName <<endl;
}

谢谢

最佳答案

#ifdef READFASTQFILE_HPP 应为 #ifndef READFASTQFILE_HPP#ifdef 导致 readFastqFile.hpp 的内容被忽略,因此不会编译类定义。

另请参阅Include guards

关于C++ 不在构造函数定义中命名类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29632476/

相关文章:

c++ - 为什么 C++ 在 C 中已经存在的 IO 中引入了新函数?

c++ - 如何使用指针从其他函数访问局部变量?

Java - 将.class文件编译为可读文件

c++ - gettimeofday() 未在此范围内声明 - Cygwin

c++ - C++ 核心库的编译错误

c++ - 执行时动态库链接

c++ - 奥尔森时区与Posix时区转换

java - 调用构造函数是否意味着创建对象?

c++ - 通过使用不同的参数调用每个元素的非默认构造函数来初始化 std::vector,而无需进行不必要的复制

c# - 使用无论值如何始终返回 0 的类时出现问题