相关疑难解决方法(0)

为什么"使用命名空间std"被认为是不好的做法?

我已经告诉别人,编写using namespace std;代码是错误的,我应该用std::coutstd::cin直接代替.

为什么被using namespace std;认为是不好的做法?是低效还是冒着声明模糊变量(与名称std空间中的函数具有相同名称的变量)的风险?它会影响性能吗?

c++ namespaces using-directives std c++-faq

2486
推荐指数
36
解决办法
78万
查看次数

如何使用迭代器?

我正在计算两点之间的距离.我用C++中的向量存储的两点:(0,0)和(1,1).

我应该得到结果

0
1.4
1.4
0
Run Code Online (Sandbox Code Playgroud)

但我得到的实际结果是

0
1
-1
0
Run Code Online (Sandbox Code Playgroud)

我认为在向量中使用迭代器的方式有问题.我该如何解决这个问题?

我发布了以下代码.

typedef struct point {
    float x;
    float y;
} point;

float distance(point *p1, point *p2)
{
    return sqrt((p1->x - p2->x)*(p1->x - p2->x) +
                (p1->y - p2->y)*(p1->y - p2->y));
}

int main()
{
    vector <point> po;
    point p1; p1.x = 0; p1.y = 0;
    point p2; p2.x = 1; p2.y = 1;
    po.push_back(p1);
    po.push_back(p2);

    vector <point>::iterator ii;
    vector <point>::iterator jj;
    for (ii = po.begin(); ii != po.end(); ii++) …
Run Code Online (Sandbox Code Playgroud)

c++ pointers iterator vector using-statement

71
推荐指数
3
解决办法
4万
查看次数

关于C++中的指针和引用的困惑

我有一堆像这样的代码:

#include <iostream>
using namespace std;

void swap(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

int main() {

    int a;
    int b;
    a = 7;
    b = 5;
    swap(a, b);
    cout << a << b;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

此代码执行交换过程,因为我完全想要交换2个数字

但是当我想要用户的两个数字时如下;

int a;
int b;
cin >> a;
cin >> b;
swap(a, b);
cout << a << b;
Run Code Online (Sandbox Code Playgroud)

编译器给我一个关于整数到*整数错误的错误,这是预期的.为什么第一个代码进行了正确的交换,尽管我没有将这个方法用于&运算符?

c++ pointers reference

21
推荐指数
1
解决办法
1万
查看次数

G ++编译器错误 - 此处首先需要合成方法

这是错误:

In file included from /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/ios:39,
                 from /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/ostream:40,
                 from /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/iostream:40,
                 from date.h:15,
                 from date.cpp:13:
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/ios_base.h: In copy constructor ‘std::basic_ios<char, std::char_traits<char> >::basic_ios(const std::basic_ios<char, std::char_traits<char> >&)’:
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/ios_base.h:790: error: ‘std::ios_base::ios_base(const std::ios_base&)’ is private
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/iosfwd:47: error: within this context
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/iosfwd: In copy constructor ‘std::basic_ostream<char, std::char_traits<char> >::basic_ostream(const std::basic_ostream<char, std::char_traits<char> >&)’:
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/iosfwd:56: note: synthesized method ‘std::basic_ios<char, std::char_traits<char> >::basic_ios(const std::basic_ios<char, std::char_traits<char> >&)’ first required here 
date.cpp: In function ‘std::ostream operator<<(std::ostream&, Date&)’:
date.cpp:389: note: synthesized method ‘std::basic_ostream<char, std::char_traits<char> >::basic_ostream(const std::basic_ostream<char, std::char_traits<char> >&)’ first required here 
make: *** [date.o] …
Run Code Online (Sandbox Code Playgroud)

c++ compiler-errors g++ header-files

7
推荐指数
1
解决办法
7634
查看次数

使用头文件和源文件时出错

我创建了一个头文件,调用它One.h来包含我的 3d 矢量类,然后我使用一个被调用的源文件main.cxx将该类输入到一个 int 函数中。但是,我不断收到这些错误,并且我已经尝试了几天来修复它,但我不能。我是 C++ 的新手。

/usr/include/c++/4.8.2/bits/stl_iterator_base_types.h:165:53:
error: no type named ‘iterator_category’ in ‘class Vector3d’
       typedef typename _Iterator::iterator_category iterator_category;
                                                     ^ 
/usr/include/c++/4.8.2/bits/stl_iterator_base_types.h:166:53: error:
no type named ‘value_type’ in ‘class Vector3d’
       typedef typename _Iterator::value_type        value_type;
                                                     ^ 
/usr/include/c++/4.8.2/bits/stl_iterator_base_types.h:167:53: error:
no type named ‘difference_type’ in ‘class Vector3d’
       typedef typename _Iterator::difference_type   difference_type;
                                                     ^ 
/usr/include/c++/4.8.2/bits/stl_iterator_base_types.h:168:53: error:
no type named ‘pointer’ in ‘class Vector3d’
       typedef typename _Iterator::pointer           pointer;
                                                     ^ 
/usr/include/c++/4.8.2/bits/stl_iterator_base_types.h:169:53: error:
no type named ‘reference’ in ‘class Vector3d’
       typedef typename _Iterator::reference         reference; …
Run Code Online (Sandbox Code Playgroud)

c++

0
推荐指数
1
解决办法
53
查看次数