我正在用C++编写一个小矩阵库来进行矩阵运算.然而,我的编译器抱怨,在它之前没有.这个代码留在架子上6个月,在我之间我将我的计算机从debian etch升级到lenny(g ++(Debian 4.3.2-1.1)4.3.2)然而我在具有相同g ++的Ubuntu系统上遇到了同样的问题.
这是我的矩阵类的相关部分:
namespace Math
{
class Matrix
{
public:
[...]
friend std::ostream& operator<< (std::ostream& stream, const Matrix& matrix);
}
}
Run Code Online (Sandbox Code Playgroud)
而"实施":
using namespace Math;
std::ostream& Matrix::operator <<(std::ostream& stream, const Matrix& matrix) {
[...]
}
Run Code Online (Sandbox Code Playgroud)
这是编译器给出的错误:
matrix.cpp:459:错误:'std :: ostream&Math :: Matrix :: operator <<(std :: ostream&,const Math :: Matrix&)'必须只取一个参数
我对这个错误感到有些困惑,但是在6个月里做了大量的Java后,我的C++又变得有点生疏了.:-)
我想在C++中使用无符号的8位变量.关于算术的任何一个unsigned char或者uint8_t做的伎俩(这是预期的,因为AFAIK uint8_t只是一个别名unsigned char,或者调试器提出它.
问题是,如果我在C++中使用ostream打印出变量,它会将其视为char.如果我有:
unsigned char a = 0;
unsigned char b = 0xff;
cout << "a is " << hex << a <<"; b is " << hex << b << endl;
Run Code Online (Sandbox Code Playgroud)
然后输出是:
a is ^@; b is 377
Run Code Online (Sandbox Code Playgroud)
代替
a is 0; b is ff
Run Code Online (Sandbox Code Playgroud)
我尝试过使用uint8_t,但正如我之前提到的,那是typedef'ed unsigned char,所以它也是如此.如何正确打印变量?
编辑:我在我的代码中的许多地方都这样做.有没有什么办法可以做到这一点,而不铸造int我想打印每一次?
double x = 1500;
for(int k = 0; k<10 ; k++){
double t = 0;
for(int i=0; i<12; i++){
t += x * 0.0675;
x += x * 0.0675;
}
cout<<"Bas ana: "<<x<<"\tSon faiz: "<<t<<"\tSon ana: "<<x+t<<endl;
}
Run Code Online (Sandbox Code Playgroud)
这个输出
Bas ana:3284.78 Son faiz:1784.78 Son ana:5069.55
Bas ana:7193.17 Son faiz:3908.4 Son ana:11101.6
Bas ana:15752 Son faiz:8558.8 Son ana:24310.8
Bas ana:34494.5 Son faiz:18742.5 Son ana:53237
Bas ana:75537.8 Son faiz:41043.3 Son ana:116581
Bas ana:165417 Son faiz:89878.7 Son ana:255295
Bas ana:362238 Son faiz:196821 Son …
我现在已经在StackOverflow.com上阅读了几个关于我的问题的问题,但似乎没有一个能解决我的问题.或者我可能做错了... <<如果我将其转换为内联函数,则重载会起作用.但是我如何让它在我的情况下工作?
warning: friend declaration std::ostream& operator<<(std::ostream&, const D<classT>&)' declares a non-template function
warning: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here) -Wno-non-template-friend disables this warning
/tmp/cc6VTWdv.o:uppgift4.cc:(.text+0x180): undefined reference to operator<<(std::basic_ostream<char, std::char_traits<char> >&, D<int> const&)' collect2: ld returned 1 exit status
代码:
template <class T>
T my_max(T a, T b)
{
if(a > b)
return a;
else
return b;
}
template <class classT> …Run Code Online (Sandbox Code Playgroud) 我正在寻找一个std::ostream类似的实现/dev/null.它会忽略流向它的任何内容.标准库或Boost中是否存在这样的事情?或者我必须自己动手?
如何使用std :: cout执行以下操作?
double my_double = 42.0;
char str[12];
printf_s("%11.6lf", my_double); // Prints " 42.000000"
Run Code Online (Sandbox Code Playgroud)
我准备放弃并使用sprintf_s.
更一般地说,我在哪里可以找到std :: ostream格式的引用,它在一个地方列出所有内容,而不是在长篇教程中全部展开?
编辑2017年12月21日 - 请参阅下面的答案.它使用了我在2012年提出这个问题时无法使用的功能.
当我运行以下程序时
#include <iostream>
int main()
{
char c = 'a';
std::cout << c << std::endl;
std::cout.operator<<(c) << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我得到了输出
a
97
Run Code Online (Sandbox Code Playgroud)
在http://en.cppreference.com/w/cpp/io/basic_ostream/operator_ltlt进一步挖掘,我注意到std::ostream::operator<<()没有char作为参数类型的重载.函数调用std::cout.operator<<(a)被解析为std::ostream::operator<<(int),这解释了输出.
我假设和operator<<之间的函数在其他地方声明为:std::ostreamchar
std::ostream& operator<<(std::ostream& out, char c);
Run Code Online (Sandbox Code Playgroud)
否则,std::cout << a将解决std::ostream::operator<<(int).
我的问题是为什么声明/定义为非成员函数?是否存在阻止其成为会员功能的已知问题?
我一直在谷歌搜索,我只是找不到一个简单的答案.它应该很简单,就像STL一般.
我想定义从std :: ostream公开继承的MyOStream.假设我想在每次将某些内容写入流中时调用foo().
class MyOStream : public ostream {
public:
...
private:
void foo() { ... }
}
Run Code Online (Sandbox Code Playgroud)
我知道ostream的公共接口是非虚拟的,那怎么办呢?我希望客户能够在MyOStream上使用operator <<和write()以及put()并使用我的类的扩展功能.
在http://channel9.msdn.com/Events/GoingNative/2013/Writing-Quick-Code-in-Cpp-Quickly的 50:40时,Andrei Alexandrescu开玩笑说如何效率/慢速istream.
我过去遇到过一个问题,ostream很慢,而且fwrite明显更快(在主循环运行一次时减少了很多秒),但我从来不明白为什么也没看过它.
什么使得C++中的istream和ostream变慢?或者至少比其他东西(如fread/fget,fwrite)慢,这同样满足了需求.
我正在学习C++.cout是一个std::ostream类的实例.如何用它打印格式化的字符串?
我仍然可以使用printf,但我想学习更多C++风格的方法,它可以带来所有C++的好处.我认为这应该是可能的,std::ostream但我找不到合适的方法.
ostream ×10
c++ ×9
cout ×2
formatting ×2
c++11 ×1
double ×1
friend ×1
inheritance ×1
iostream ×1
istream ×1
namespaces ×1
null ×1
performance ×1
printf ×1
stl ×1
templates ×1