Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/161.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 已定义错误_C++_Visual C++ - Fatal编程技术网

C++ 已定义错误

C++ 已定义错误,c++,visual-c++,C++,Visual C++,嗨,我刚刚创建了一个示例类,并在main中使用它,但我得到了已经定义的错误 sample.h #ifndef __sample__ #define __sample__ #include<iostream> using namespace std; int count = 10; class sample { public: sample(); int Get(); private: int i; }; #endif \ifndef\u示例__ #定义样

嗨,我刚刚创建了一个示例类,并在main中使用它,但我得到了已经定义的错误

sample.h

#ifndef __sample__
#define __sample__
#include<iostream>
using namespace std;

int count = 10;

class sample
{
public:
    sample();
    int Get();
private:
    int i;
};
#endif
\ifndef\u示例__
#定义样本__
#包括
使用名称空间std;
整数计数=10;
类样本
{
公众:
样本();
int Get();
私人:
int i;
};
#恩迪夫
sample.cpp

#include "sample.h"
sample::sample()
{
    cout<<"hello two";
}
int sample::sample()
{
    return 10;
}
#include <iostream>
#include "sample.h"
using namespace std;

int main(void)
{
    int test = count;
    return 0;
}
#包括“sample.h”
sample::sample()
{

coutInclude guard仅防止多次包含同一头文件,而不是多个定义。应在cpp文件中移动变量,以避免违反,或使用内部链接或将其声明为外部并在某个位置定义一次。根据该变量的使用情况,有多种解决方案

请注意,我忽略了一个事实,即sample.cpp文件中的
intsample::Get()

#include "sample.h"
sample::sample()
{
    cout<<"hello two";
}
int sample::sample() // ??
{
    return 10;
}
#包括“sample.h”
sample::sample()
{

cout要使这样的全局变量随处可见:

废话

extern int count;
胡说八道

int count(10);

例如,您必须声明变量count具有内部链接

#ifndef __sample__
#define __sample__
#include<iostream>
using namespace std;

namespace
{
    int count = 10;
}
//...

#endif
\ifndef\u示例__
#定义样本__
#包括
使用名称空间std;
名称空间
{
整数计数=10;
}
//...
#恩迪夫

(上述内部声明在C++ 2011中有效)或

\ifndef\u示例__
#定义样本__
#包括
使用名称空间std;
静态整数计数=10;
//...
#恩迪夫
或者声明它具有外部链接,但在某个module.Fpr示例中只定义一次

#ifndef __sample__
#define __sample__
#include<iostream>
using namespace std;

extern int count;

//...

#endif

#include "sample.h"

int count = 10;
sample::sample()
{
    cout<<"hello two";
}
int sample::sample()
{
    return 10;
}
\ifndef\u示例__
#定义样本__
#包括
使用名称空间std;
外部内部计数;
//...
#恩迪夫
#包括“sample.h”
整数计数=10;
sample::sample()
{
请记住,
#include
的字面意思是“在此处添加此文件的内容”。
Include guards仅针对每个包含文件的文件多次包含文件内容进行保护

预处理器完成预处理后,编译器会看到:

sample.cpp

#include "sample.h"
sample::sample()
{
    cout<<"hello two";
}
int sample::sample()
{
    return 10;
}
#include <iostream>
#include "sample.h"
using namespace std;

int main(void)
{
    int test = count;
    return 0;
}
[此处为iostream内容…]

using namespace std;

int count = 10;

class sample
{
public:
    sample();
    int Get();
private:
    int i;
};

sample::sample()
{
    cout<<"hello two";
}
int sample::sample()
{
    return 10;
}
using namespace std;

int count = 10;

class sample
{
public:
    sample();
    int Get();
private:
    int i;
};

using namespace std;

int main(void)
{
    int test = count;
    return 0;
}
如您所见,
count
有两个定义,每个文件中有一个(正式称为“翻译单位”)

解决方案是在“sample.h”中声明变量

并在sample.cpp中具有唯一的定义:

int count = 10;

(你不应该把
使用namespace std;
放在标题中。)

我相信你的意思是
intsample::Get()
\uuuuu sample\uuuuuuuuu
是一个保留标识符。有多个问题需要解决,不仅仅是定义问题。@T.C.编辑消息使我感到可笑,将非常量变量放在匿名名称空间/静态标题中的重复可能会引起混淆,因为每个翻译单元都有自己的副本。@Dav没有什么混乱。这取决于你的设计,比我弄得一团糟要好得多。