【发布时间】:2016-02-08 03:32:55
【问题描述】:
所以我正在尝试编译一个非常简单的项目。由于某些原因,它在 .cpp 文件中找不到我的课程。
代码如下: main.cpp:
#include <iostream>
#include "Dog.h"
using namespace std;
int main()
{
Dog myDog;
return 0;
}
狗.h:
#ifndef DOG_H
#define DOG_H
class Dog
{
public:
Dog();
virtual ~Dog();
Dog(const Dog& other);
Dog& operator=(const Dog& other);
protected:
private:
};
#endif // DOG_H
狗.cpp:
#include "Dog.h"
Dog::Dog()
{
//std::cout << "I'm alive!";
}
Dog::~Dog()
{
//dtor
}
Dog::Dog(const Dog& other)
{
//copy ctor
}
Dog& Dog::operator=(const Dog& rhs)
{
if (this == &rhs) return *this; // handle self assignment
//assignment operator
return *this;
}
所以这是非常基本的,但我仍然收到错误:'Dog' is not declared in this scope.
我认为我需要将其添加到构建中,但我已经通过右键单击项目窗口中的 dog.cpp 并构建设置来做到这一点。
编译器日志:
-------------- Build: Debug in MyProject (compiler: GNU GCC Compiler)---------------
mingw32-g++.exe -Wall -fexceptions -g -Iinclude -c C:\Users\tamas\Documents\MyProject\include\Dog.cpp -o obj\Debug\include\Dog.o
mingw32-g++.exe -Wall -fexceptions -g -Iinclude -c C:\Users\tamas\Documents\MyProject\main.cpp -o obj\Debug\main.o
C:\Users\tamas\Documents\MyProject\main.cpp: In function 'int main()':
C:\Users\tamas\Documents\MyProject\main.cpp:9:5: error: 'Dog' was not declared in this scope
Dog myDog;
^
C:\Users\tamas\Documents\MyProject\main.cpp:9:9: error: expected ';' before 'myDog'
Dog myDog;
^
【问题讨论】:
-
include "Dog.h"我认为这是复制/粘贴错误。 -
是的,# 字符确实存在!
-
在 what 范围内到底 Dog 没有被定义? (编译器应该告诉你的)
-
如果 Dog.cpp 不在构建中,我怀疑您会遇到未解决的外部符号错误。
-
不,Dog.cpp 不是问题。 Dog.h 是问题所在 - 然而,它的内容似乎还不错。所以你正在编译一些其他的 Dog.h。
标签: c++ codeblocks mingw32