gpt4 book ai didi

'(' 标记之前的 C++ 错误预期主表达式

转载 作者:搜寻专家 更新时间:2023-10-31 00:54:58 25 4
gpt4 key购买 nike

我正在尝试创建一个程序,该程序从用户输入中获取 N 个随机节点,并创建一个随机整数,将其放入二叉树中,然后复制到优先级队列中。整数成为每个节点的键,另一个整数计算键的频率。当我复制到优先级队列时遇到问题,因为我得到重复项并且我需要删除它们。我试图通过节点构造函数创建一个集合,但在 .cpp 文件中出现上述错误。

#include <iostream>
#include <random>
#include <ctime>
#include <queue>
#include <set>
#include <functional>
#include <algorithm>
#include<list>
#include "Q7.h"

using namespace std;

int main()
{
node * root=NULL;
node z;
int n,v;

vector<int> first;
vector<int>::iterator fi;

default_random_engine gen(time(NULL));
cout<<"how many values? "; cin>>n;

for(int i=0; i<n; i++)
{ (v=gen()%n);
first.push_back(v);

if(root==NULL){root = node(set(v));}///This is where I get the error!!
else{
root->addnode(v);
}
}

z.unsortedRemoveDuplicates(first);

cout<<"Binary Tree in a depth first manner with Duplicates removed!"<<endl;
for ( fi = first.begin() ; fi != first.end(); ++fi{cout<<"Node "<<*fi<<endl;}
cout<<"-------------------"<<endl;
root->display();
cout<<"-------------------"<<endl;
cout<<"-------------------"<<endl;
root->display_Queue1();
cout<<"-------------------"<<endl;

return 0;
}





my .h file

class node
{
public:
node(){left=NULL; right=NULL; ct = 1;}
node set(int v) {val = v; left=NULL; right=NULL; ct=1;}
node (int Pri, int cat)
: val(Pri), ct(cat) {}

friend bool operator<(//sorts queue by lowest Priority
const node& x, const node& y) {
return x.val < y.val;
}

friend bool operator>(//sorts queue by greatest Priority
const node& x, const node& y) {
return x.ct > y.ct;
}

friend ostream&//prints out queue later
operator<<(ostream& os, const node& Pri) {
return os <<"my value = "<<Pri.val<<" occured "<<Pri.ct<<" times";
}

int unsortedRemoveDuplicates(vector<int>& numbers)
{
node set<int> seenNums; //log(n) existence check
auto itr = begin(numbers);
while(itr != end(numbers))
{
if(seenNums.find(*itr) != end(seenNums)) //seen? erase it
itr = numbers.erase(itr); //itr now points to next element
else
{
seenNums.insert(*itr);
itr++;
}
}
return seenNums.size();
}
priority_queue<node, vector<node>, greater<node> > pq;
priority_queue<node, vector<node>, less<node> > pq1;

void addnode(int v)
{
if(v==val){ct++;}
pq.emplace(node (set (v)));///No error here for set with constructor why??
pq.emplace(node (set (v)));
if(v<val)
{
if(left==NULL){left=new node(set(v));
}
else{left->addnode(v);
}
}
else
{
if(right==NULL){right = new node (set(v));
}
else{right->addnode(v);
}
}
}

int display()
{
if(left!=NULL){left->display();}

cout<<"frequency "<<ct<<" value"<<val<<endl;

if(right!=NULL){right->display();}
}

void display_Queue()
{
cout << "0. size: " << pq.size() << '\n';
cout << "Popping out elements from Pqueue..."<<'\n';
while (!pq.empty())
{
cout << pq.top() << endl;
pq.pop();
}
cout << '\n';
}
void display_Queue1()
{
cout << "0. size: " << pq1.size() << '\n';
cout << "Popping out elements from Pqueue..."<<'\n';
while (!pq1.empty())
{
cout << pq1.top() << endl;
pq1.pop();
}
cout << '\n';
}

private:
int val; ///value in that node
int ct;
///ct = count of that value
node * left;
node * right;
};

最佳答案

恭喜,这一行:

root = node(set(v));

你已经发现为什么这里的人经常说 avoid using using namespace std; .这被解释为:

root = static_cast<node>(std::set(v));

而不是你想要的,这可能是:

root = new node();
root->set(v);

首先,请注意,我们需要使用 new,因为我们正在创建一个新的 node,而不是尝试将 node 转换为 node,这也会给出另一个关于尝试为指针赋值的编译器错误。

接下来,请注意,您不会在头文件中收到错误,因为那里没有 using namespace std;,并且由于它在成员函数中,因此行:

void node::addnode(int v)
{
//...
pq.emplace(node (set (v)));///No error here for set with constructor why??
//...
}

被解释为:

pq.emplace(static_cast<node>(this->set(v)));

但是,这是您真正想要做的吗?


此外,我会将构造函数更改为:

public:
node (int Pri = 0, int cat = 1)
: val(Pri), ct(cat), left(NULL), right(NULL) {}
// DELETED node (int Pri, int cat)

因此你可以这样做:

root = new node(v);

它会像我认为的那样工作。

关于 '(' 标记之前的 C++ 错误预期主表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43623925/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com