小编don*_*age的帖子

Python随机函数

我遇到Python的导入随机函数问题.似乎import random并且from random import random正在导入不同的东西.我目前正在使用Python 2.7.3

Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> random()

Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
    random()
NameError: name 'random' is not defined
>>> random.randint(1,5)

Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
random.randint(1,5)
NameError: name 'random' is not defined
>>> import random
>>> random()

Traceback (most recent …
Run Code Online (Sandbox Code Playgroud)

python random import

10
推荐指数
4
解决办法
6万
查看次数

单独文件中的 C++ 类

我正在尝试学习如何在 C++ 中创建类,其中使用头文件、包含类函数定义的 .cpp 文件和主 .cpp 文件。这是我所拥有的(取自示例)

在类.h中

class MyClass
{
public:
  void foo();
  int bar;
};
Run Code Online (Sandbox Code Playgroud)

在类.cpp中

#include "class.h"
using namespace std;
void MyClass::foo()
{
    cout<< "test";
}
Run Code Online (Sandbox Code Playgroud)

在main.cpp中

#include "class.h" 
using namespace std;
int main()
{
  MyClass a;
  a.foo();
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

编译 main.cpp 会导致以下错误: [链接器错误] C:\:(.text+0x16): undefined reference to `MyClass::foo()'collect2: ld returned 1 exit status

我需要编译class.cpp或class.h吗?我是否缺少将 class.h 与 class.cpp 链接的方法?如果是这样我该如何链接它们?

c++ class

7
推荐指数
2
解决办法
3万
查看次数

标签 统计

c++ ×1

class ×1

import ×1

python ×1

random ×1