xcode 对象未在范围内声明

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2402636/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 19:10:23  来源:igfitidea点击:

Object not declared in scope

c++visual-studioxcode

提问by jumana

I'm using Xcode for C++ on my computer while using Visual Studio at school. The following code worked just fine in Visual Studio, but I'm having this problem when using Xcode.

在学校使用 Visual Studio 时,我在计算机上使用 Xcode for C++。下面的代码在 Visual Studio 中工作得很好,但我在使用 Xcode 时遇到了这个问题。

clock c1(2, 3, 30);

Everything works just fine, but it keeps giving me this error that says "Expected ';' before 'c1'"

一切正常,但它不断给我这个错误,上面写着“预期的';' 在‘c1’之前”

Fine, I put the ';' .. but then, it gives me this error: "'c1' was not declared in this scope"

好吧,我把';' ..但是,它给了我这个错误:“'c1'没有在这个范围内声明”

Here's the whole header code:

这是整个标题代码:

#include <iostream>
using namespace std;

class clock
{
private:
 int h;
 int m;
 int s;

public: 
 clock(int hr, int mn, int sec);
};

clock::clock(int hr, int mn, int sec)
{
 h = hr;
 m = mn; 
 s = sec;
}

Here's the whole .cpp code:

这是整个 .cpp 代码:

#include "clock.h"

int main()
{
    clock c1(2, 3, 30);
    return 0;
}

I stripped everything down to where I had the problem. Everything else, as far as I know, is irrelevant since the problem remains the same with just the mentioned above.

我把所有东西都精简到我遇到问题的地方。据我所知,其他一切都无关紧要,因为问题与上面提到的一样。

Thanks in advance!

提前致谢!

回答by Johannes Schaub - litb

There is a function clockthat will hide your clockclass of the same name. You can work this around by saying

有一个函数clock可以隐藏您clock的同名类。你可以通过说来解决这个问题

class clock c1(2, 3, 30);

It's very bad practice to do using namespace std;in a header. Instead put that line into the cppfile only. It may solve your problem if you remove that line (if the name comes from namespace std::instead of from the global namespace originally).

using namespace std;在标题中做是非常糟糕的做法。而是cpp只将该行放入文件中。如果删除该行(如果名称来自命名空间std::而不是最初来自全局命名空间),它可能会解决您的问题。

回答by Peter Alexander

Your clock class definition is clashing with clock_t clock()from <ctime>(see here).

您的时钟类定义与clock_t clock()from <ctime>(请参阅此处)发生冲突。

You can get around it by specifying that you want the class and not the function in the way that Johannes described, but really you should just rename your class so that it doesn't clash with a standard function. That's the most practical solution.

你可以通过指定你想要的类而不是 Johannes 描述的函数的方式来解决它,但实际上你应该只重命名你的类,这样它就不会与标准函数发生冲突。这是最实用的解决方案。

Just to reiterate what Johannes said, do not put using namespace std;in a header. That causes the stdnamespace to be injected into every file that includes your header, which is bound to cause identifier collisions at some point. If you really need it, but it in the source file only, as nothing includes that.

只是重申约翰内斯所说的话,不要放在using namespace std;标题中。这会导致std命名空间被注入到包含您的标头的每个文件中,这必然会在某些时候导致标识符冲突。如果你真的需要它,但它只在源文件中,因为没有包含它。

回答by quamrana

You should put your clockclass into a namespace to stop it conflicting with clock().

您应该将您的clock类放入命名空间中以防止它与clock().

Well done for stripping everything else out before asking the question.

在提出问题之前剥离所有其他内容做得很好。

Since you have stripped lots of things out, you will be putting it all back again. There may be other problems that you encounter:

既然你已经剥离了很多东西,你就会把它全部放回去。您可能会遇到其他问题:

  • As others have said: do not put using namespace std;in a header.
  • You will need header guards if you hadn't already got them.
  • You should move the constructor implementation back into a module file, or make it inline, because the linker will complain.
  • 正如其他人所说:不要放入using namespace std;标题。
  • 如果你还没有得到他们,你将需要他们。
  • 您应该将构造函数实现移回模块文件,或使其内联,因为链接器会抱怨。