我可以输入输入,但是当我在 visual studio 2017 年 运行 我的 C++ 代码 (CTRL+F5) 时,我无法看到输出
I can enter the inputs but I'm not able to see the output when I run my code (CTRL+F5) in C++ in visual studio 2017
I can enter the inputs but I'm not able to see the output when I run my code (CTRL+F5) in C++ in visual studio 2017. This code is written to overload the operator *, ">>" & "<<" through the friend functions. one constructor initializes the vector elements to zero while the other constructor is used to initialize the elements of vector through the array.
我的代码如下:
//vector.h
#pragma once
const int SIZE = 3;
#include<iostream>
using namespace std;
class vector
{
int v[SIZE];
public:
vector();
vector(int *x);
friend vector operator *(vector a, int b);
friend vector operator *(int b,vector a);
friend istream & operator >>(istream &input, vector &x);
friend ostream & operator <<(ostream &output, vector &y);
};
//vector.cpp
#include "vector.h"
#include<iostream>
using namespace std;
vector::vector()
{
for (int i = 0; i < SIZE; i++)
v[i] = 0;
}
vector::vector(int *x)
{
for (int i = 0; i < SIZE; i++)
v[i] = x[i];
}
vector operator *(vector a, int b) {
vector temp;
for (int i = 0; i < SIZE; i++)
temp.v[i] = a.v[i]*b;
return temp;
}
vector operator *(int a, vector b) {
vector temp;
for (int i = 0; i < SIZE; i++)
temp.v[i] = a * b.v[i];
return temp;
}
istream & operator >>(istream &input, vector &x) {
for (int i = 0; i < SIZE; i++)
input >> x.v[i];
return (input);
}
ostream & operator <<(ostream &output, vector &y) {
output << "(" << y.v[0];
for (int i = 1; i < SIZE; i++)
output <<","<< y.v[i];
output << ")";
return (output);
}
//main.cpp
#include "vector.h"
#include<iostream>
using namespace std;
int x[SIZE] = { 2,4,6 };
int main() {
vector m;
vector v2 = x;
cout << "Enter the elements of vector m:" << "\n";
cin >> m;
cout << "\n";
vector m1, m2;
m1 = m * 2;
m2 = 2 * v2;
cout << "m= "<<m<<"\n";
cout << "\n";
cout << "m1= " << m1 << "\n";
cout << "m2= " << m2 << "\n";
cin.get();
return 0;
}
您的代码假定您必须输入 3 个数字。如果输入的数字较少,则看不到任何输出。
一些评论:
您的 cin.get() 不会停止执行。
使用 std(就像使用命名空间 std)和定义自己的向量 class - 这是非常糟糕的主意。
可能是您的链接器设置不正确。转到菜单项 Project/Properties。然后在对话框中找到 Configuration Properties/Linker/System/Subsystem 并确保它显示 Console (/SUBSYSTEM:CONSOLE)
.
Evgeny 是正确的 cin.get()
不能保证停止您的程序,因为仍然有字符等待从 cin
中读取,这些字符是以前读取时遗留下来的。
I can enter the inputs but I'm not able to see the output when I run my code (CTRL+F5) in C++ in visual studio 2017. This code is written to overload the operator *, ">>" & "<<" through the friend functions. one constructor initializes the vector elements to zero while the other constructor is used to initialize the elements of vector through the array.
我的代码如下:
//vector.h
#pragma once
const int SIZE = 3;
#include<iostream>
using namespace std;
class vector
{
int v[SIZE];
public:
vector();
vector(int *x);
friend vector operator *(vector a, int b);
friend vector operator *(int b,vector a);
friend istream & operator >>(istream &input, vector &x);
friend ostream & operator <<(ostream &output, vector &y);
};
//vector.cpp
#include "vector.h"
#include<iostream>
using namespace std;
vector::vector()
{
for (int i = 0; i < SIZE; i++)
v[i] = 0;
}
vector::vector(int *x)
{
for (int i = 0; i < SIZE; i++)
v[i] = x[i];
}
vector operator *(vector a, int b) {
vector temp;
for (int i = 0; i < SIZE; i++)
temp.v[i] = a.v[i]*b;
return temp;
}
vector operator *(int a, vector b) {
vector temp;
for (int i = 0; i < SIZE; i++)
temp.v[i] = a * b.v[i];
return temp;
}
istream & operator >>(istream &input, vector &x) {
for (int i = 0; i < SIZE; i++)
input >> x.v[i];
return (input);
}
ostream & operator <<(ostream &output, vector &y) {
output << "(" << y.v[0];
for (int i = 1; i < SIZE; i++)
output <<","<< y.v[i];
output << ")";
return (output);
}
//main.cpp
#include "vector.h"
#include<iostream>
using namespace std;
int x[SIZE] = { 2,4,6 };
int main() {
vector m;
vector v2 = x;
cout << "Enter the elements of vector m:" << "\n";
cin >> m;
cout << "\n";
vector m1, m2;
m1 = m * 2;
m2 = 2 * v2;
cout << "m= "<<m<<"\n";
cout << "\n";
cout << "m1= " << m1 << "\n";
cout << "m2= " << m2 << "\n";
cin.get();
return 0;
}
您的代码假定您必须输入 3 个数字。如果输入的数字较少,则看不到任何输出。
一些评论:
您的 cin.get() 不会停止执行。 使用 std(就像使用命名空间 std)和定义自己的向量 class - 这是非常糟糕的主意。
可能是您的链接器设置不正确。转到菜单项 Project/Properties。然后在对话框中找到 Configuration Properties/Linker/System/Subsystem 并确保它显示 Console (/SUBSYSTEM:CONSOLE)
.
Evgeny 是正确的 cin.get()
不能保证停止您的程序,因为仍然有字符等待从 cin
中读取,这些字符是以前读取时遗留下来的。