我已经告诉别人,编写using namespace std;代码是错误的,我应该用std::cout和std::cin直接代替.
为什么被using namespace std;认为是不好的做法?是低效还是冒着声明模糊变量(与名称std空间中的函数具有相同名称的变量)的风险?它会影响性能吗?
关于std命名空间使用'using'似乎有不同的看法.
有人说使用' using namespace std',其他人说不要,而是先加上与' std::' 一起使用的std函数,而其他人则说使用这样的东西:
using std::string;
using std::cout;
using std::cin;
using std::endl;
using std::vector;
Run Code Online (Sandbox Code Playgroud)
对于要使用的所有std函数.
各自的优点和缺点是什么?
是否需要#include某个文件,如果在标题(*.h)内,使用此文件中定义的类型?
例如,如果我使用GLib并希望gchar在我的标题中定义的结构中使用基本类型,是否有必要执行a #include <glib.h>,知道我已经在我的*.c文件中有它?
如果是,我还必须把它放在#ifndef和#define之后#define?
我收到这个错误,当我在 *h 和 *cpp 中分离一个文件时,以下代码未在此范围内声明“向量”这是main.cpp:
#include <iostream>
#include <math.h>
#include <vector>
#include "functia.h"
using namespace std;
int main()
{
vector<double> s(3);
double b= 4;
fun(s, b);
cout<<s[0]<<endl;
double c= 9;
fun(s, c);
cout<<s[0];
}
Run Code Online (Sandbox Code Playgroud)
功能.h:
void fun(vector<double> & rS, double a)
{
rS[0] = a + 3;
rS[1] = 4;
rS[2] = 5;
}
Run Code Online (Sandbox Code Playgroud)
功能.cpp:
#include <iostream>
#include <math.h>
#include<vector>
using namespace std;
void fun(vector<double> &, double );
Run Code Online (Sandbox Code Playgroud) 我的简单类不会在Visual Studio中编译.在我将字符串公司成员和getter方法getCo()添加到它之前,它工作正常.我想我需要将#include字符串标准库放在某处,但我不知道在哪里.知道在哪里?在我的头文件中,我有:
#pragma once
#ifndef ENGINEER_H_
#define ENGINEER_H_
class engineer {
int years;
string company;
public:
engineer(int years);
~engineer(void);
int getYears();
string getCo();
};
#endif ENGINEER_H_
Run Code Online (Sandbox Code Playgroud)
在我的CPP文件中定义类,我有:
#include "StdAfx.h"
#include "engineer.h"
engineer::engineer(int y, string c){
years = y;
company = c;
}
engineer::~engineer(void) {
}
int engineer::getYears() {
return years;
}
string engineer::getCo() {
return company;
}
Run Code Online (Sandbox Code Playgroud) 似乎我找不到一种方法来包含<QtCharts/QLineSeries>在我的标题中,所以Qt知道QLineSeries类.
Qt += charts到我的.pro文件中.#include <QtCharts/QLineSeries>了MainWindow.hRelease,通过右键单击项目并选择它并运行它来运行QMake.我收到消息:
...\PlottingCharts\mainwindow.h:14: error: 'QLineSeries' does not name a type
QLineSeries *series;
^
Run Code Online (Sandbox Code Playgroud)
很明显,Qt对QLineSeries一无所知.
作为参考,这些linechart和openglseries示例正常工作.
谁知道我错过了什么?
我正在尝试map::erase()使用以下代码测试 C++ :
//file user.h
#include <string>
#include <fstream>
#include <cstring>
using namespace std;
class User {
string name;
int id;
public:
User(const string& name, int id) : name(name), id(id) {}
int getID() const {return id;}
~User(){}
};
//file main.cpp
#include "user.h"
using namespace std;
typedef map<string, User*> Dict;
int main()
{
Dict dict;
dict["Smith"] = new User("Smith", 666); //Id = 666
dict["Adams"] = new User("Adams", 314); //Id = 314
auto it = dict.find("Adams"); //look for user 'Adams' …Run Code Online (Sandbox Code Playgroud) 有没有一种简化的方法来包含更多的命名空间,而不是每次都输入相同的东西。这很烦人,尤其是在 .h 文件中。
例如:
而不是写:
int f() {
using namespace blabla1;
using namespace blabla2;
using namespace blabla3;
}
Run Code Online (Sandbox Code Playgroud)
我会选择:
使用 myNamespace = blabla1, blabla2, blabla3;
int f() {
using namespace myNamespace;
/// this will be equivalent to the previous example
}
Run Code Online (Sandbox Code Playgroud)
谢谢
c++ ×8
namespaces ×4
header-files ×2
c ×1
c++-faq ×1
c++11 ×1
dictionary ×1
erase ×1
qt ×1
qtcharts ×1
std ×1
vector ×1
visual-c++ ×1