我想限定它可用于任意类型(例如自定义常量float,double等).例如,假设我希望定义一个值为pi的常量.
显而易见的解决方案是使用#define pi 3.14159265359,但后来pi不会在命名空间中,我冒着名称冲突的风险.我没有使用C++ 14所以我不能使用变量模板.我能想到这样做的最好方法如下:
#include <iostream>
using namespace std;
namespace constants {
template<typename T> T pi() {
return 3.14159265359;
}
}
int main() {
float pitest = 0;
pitest = constants::pi<float>();
cout << pitest << endl;
cout << constants::pi<long double>() << endl;
cout << constants::pi<int>() << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我现在可以在命名空间中定义这些常量,我可以根据需要使用任意(数字)类型.但是,至少有两个不良特征:
pitest = constants::pi<float>();而不是简单,pitest = constants::pi();即使pitest显然是一个float.有一个更好的方法吗?
这是一个有效的代码即使是有冲突最大,全局变量和MAX(INT,INT) ,内部功能空间std.
为什么没有错误?
using namespace std;
int max;
int main()
{
int c;
c=max;
//c=max(5,3);
}
Run Code Online (Sandbox Code Playgroud) 我有一个命名空间问题,这让我很疯狂.这里有一个名为"Utility"的命名空间,由我在我的一些代码中使用.一个文件只给我一些问题.这个文件包含<algorithm>,我必须拥有它.我尝试了各种重新排列和其他随机的东西来试图摆脱它.我不能为我的生活找出为什么stl_algo.h有这个问题.
/usr/include/c++/4.6/bits/stl_algo.h: In function ‘void std::random_shuffle(_RAIter, _RAIter)’:
/usr/include/c++/4.6/bits/stl_algo.h:5152:40: error: ‘std::Utility’ has not been declared
Run Code Online (Sandbox Code Playgroud) 首先,这是不一样的这个确切的话题很多高upvoted问题,除非我失去了其中的一个.所有这些都指出问题是我有一个与类同名的命名空间.这是不是的情况下(但它).
我开始创建一个名为的新控制台应用程序BatchResizer并在其中放置几个类,但后来决定将其移动到一个名为库的类库中BatchResizer.Components; 然后我将原始控制台应用程序重命名为BatchResizer.ConsoleRunner,将该项目中的所有类更改为namespace BatchResizer.ConsoleRunner.[...],将程序集名称和默认名称空间设置为相同.
有一个标题为类的类,但项目BatchResizer中没有命名空间[...].BatchResizer,但是当我这样做时,var batchResizer = new BatchResizer()我得到的错误是命名空间被用作类.有是命名为喜欢的项目BatchResizer.ConsoleRunner.[...]或BatchResizer.Components.[...],但在没有结束BatchResizer.
我试过"清理"并重新制作项目,删除.suo文件,删除/bin解决方案中所有项目的文件夹,并且我已经浏览了所有相关项目中的每个类的命名空间冲突.
我试着做朋友的ostream功能.编译器说我无法访问该类的私有成员,即使我声明它friend.我读了一个类似的问题,它说问题出在namespcaes上.(问题:C++友好函数无法访问私有成员)
我的代码如下:
标题:
#include <iostream>
#include <string>
//using namespace std;
namespace biumath
{
#ifndef BIUMATH_H
#define BIUMATH_H
class Assignment
{
private:
int **m_varArray;
int m_rowsOfVarArray;
public:
Assignment(); //1
Assignment(char symbol, double value); //2
bool containsValueFor(char symbol) const; //3
double valueOf(char symbol) const; //4
void add(char symbol, double val); //5
friend std::ostream& operator<< (std::ostream& out,
const Assignment& assignment);
};
}
#endif
Run Code Online (Sandbox Code Playgroud)
CPP:
#include <iostream>
#include "biumath.h"
using namespace biumath;
using std::cout;
using std::endl;
ostream& operator<< …Run Code Online (Sandbox Code Playgroud) 这是我对C#感到愤怒的事情之一.我现在正在尝试使用大量库,由于某种原因,创建代码示例的人不够智能,无法在示例中包含您需要导入的名称空间.这是通用的情况.我发现自己正在搜索命名空间,数百个有时嵌套试图找出我需要导入的名称空间.在java netbeans中,它甚至会告诉我要导入哪些包,因为它会搜索我.但是在C#中我总是浪费无数次手动搜索命名空间.
有什么方法可以绕过这个.就像现在我试图找到哪个命名空间包含Tweetinvi库的TwitterCredentials.
说真的,为什么人们不在代码示例中包含命名空间.这不是愚蠢的!为什么visual studio不会像java那样提出建议.这真的只是常识....
我遇到了一个我无法理解的奇怪的编译错误。首先,错误将函数引用为该函数,就好像该函数位于匿名名称空间中一样,但实际上是在内部namespace database。其次,“已使用但从未定义”语句表明编译要求我从头文件中定义函数。该函数实际上是在单独的实现文件中声明的。但是该函数既不是静态的也不是内联的,所以我对为什么它需要在标头中定义感到困惑。这段代码是严格符合要求的,因此我已经使用二者进行了编译-Wall -Werror。我还提供了源代码的简化版本以进行说明。
注意:此问题与此处提出的其他类似问题不同,因为它不涉及静态或内联函数。
错误:
In file included from src/main.cpp:6:0:
include/database.hpp:19:6: error: 'void {anonymous}::SetupSettings()' used but never defined [-Werror]
void SetupSettings();
^
cc1plus.exe: all warnings being treated as errors
Run Code Online (Sandbox Code Playgroud)
main.cpp
#include <iostream>
#include "config.hpp"
#include "database.hpp"
int main() {
database::SetupSettings();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
数据库.hpp
#ifndef database
#define database
#include <iostream>
#include "config.hpp"
#include "sqlite/sqlite3.h"
namespace database {
extern sqlite3* settings_database;
void SetupSettings();
// ^^ Apparent warning here.
} // namespace database
#endif
Run Code Online (Sandbox Code Playgroud)
database.cpp:
#include <iostream> …Run Code Online (Sandbox Code Playgroud) 我正在写一个矩阵库.我将我的类放在命名空间SLMath中.但由于内联函数我得到错误.
这些是我的文件..
Mat4.hpp
#ifndef INC_SLMATH_MAT4_H
#define INC_SLMATH_MAT4_H
#include<cstdint>
#include<algorithm>
namespace SLMath
{
class Mat4
{
typedef std::uint8_t uint8; // You know that Its tedious to write std::uint8_t everytime
float matrix[16];
inline int index(uint8 row,uint8 col) const;
public:
//Constructors
Mat4();
Mat4(const Mat4 &other);
//Destructor
~Mat4();
//operators
void operator=(const Mat4 &other);
//loads the identity matrix
void reset();
//returns the element in the given index
inline float operator()(uint8 row,uint8 col) const;
//returns the matrix array
inline const float* const valuePtr();
};
}
#endif …Run Code Online (Sandbox Code Playgroud) ob_start();
require_once '\dompdf\autoload.inc.php';
use Dompdf\Dompdf;
//use Dompdf\Dompdf;
// instantiate and use the dompdf class
$dompdf = new DOMPDF();
$html = "
print_r($_POST);
";
$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'landscape');
$dompdf->render();
$pdf = $dompdf->output();
file_put_contents("page.pdf", $pdf);
?>
<a href="./page.pdf" download="page.pdf">Download the pdf</a>
<?php
exit;
?>
Run Code Online (Sandbox Code Playgroud)
我尝试做可下载的PDF脚本,但解析错误。
我是C++的初学者.据我所知,为了使用名称,我们必须包含由该名称组成的库.此后,我们可以预先添加命名空间的名称或使用using关键字.
例如
不使用关键字:
std::cout << "Hello Word!" << std::endl;
Run Code Online (Sandbox Code Playgroud)
使用关键字:
using namespace std;
cout << "Hello World!" << endl;
Run Code Online (Sandbox Code Playgroud)
我在网上看到了一个使用isalpha名称空间中locale库名称的在线代码示例std.但是,该样本不使用上述任何方法.
例如
#include <iostream>
#include <locale>
int main() {
std::cout << isalpha('a') << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
有人可以向我解释为什么代码仍然有效吗?