[英]Not recognizing cout<< in my program?
我正在創建一個包含多個文件的程序,但無法在我的tnode文件中識別cout <<。 誰能找到問題所在? 除其他錯誤外,我在節點文件中收到此錯誤“在此作用域中未聲明cout”。 我的主要功能:
#include <iostream>
#include "bst.h"
using namespace std;
int main(int argc, char *argv[]) {
cout<<"hi";
bst *list = new bst();
return 0;
}
我的BinarySearchTree文件:
#ifndef bst_H
#define bst_H
#include <iostream>
#include <string>
#include "tnode.h"
class bst
{
public:
bst()
{
root = NULL;
}
void add(int key, char value) {
if (root == NULL) {
root = new tnode(key, value);
return
} else
root->add(key, value);
return
}
tnode *root;
};
#endif
我的節點文件:
#ifndef tnode_H
#define tnode_H
#include <iostream>
#include <string>
class tnode
{
public:
tnode(int key, char value)
{
this->key = key;
this->value = value;
N = 1;
left = NULL;
right = NULL;
cout<<"hi";
}
void add(int key, char value) {
if (key == this->key)
{
cout<<"This key already exists";
return;
}
else if (key < this->key)
{
if (left == NULL)
{
left = new tnode(key, value);
cout<<"Your node has been placed!!";
return;
}
else
{
left->add(key, value);
cout<<"Your node has been placed!";
return;
}
}
else if (key > this->key)
{
if (right == NULL)
{
right = new tnode(key, value);
cout<<"Your node has been placed!!"; return;
}
else
return right->add(key, value);
}
return;
}
tnode* left;
tnode* right;
int key;
char value;
int N;
};
#endif
您需要做:
using namespace std;
要么
std::cout
在您的tnode
文件中
但是using namespace std
被認為是不好的做法,因此,最好使用第二種方法:
std::cout<<"Your node has been placed!!";
您需要使用命名空間std
。 通過using namespace std
(可以在.cpp文件中使用,但不能在.h文件中使用,請在此處詳細了解原因),或在調用它時使用std::cout
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.