对CLASS :: function()的未定义引用(undefined reference to CLASS::function())
因此,当我尝试使用“g ++ Asg5.cpp”简单地编译我的代码时,我收到以下错误
/tmp/cczhpSGO.o:在函数'main'中:
Asg5.cpp :(。text + 0x2fb):对'BinomialTree :: insert(int)'的未定义引用
collect2:ld返回1退出状态
如果有人想知道我为什么不使用makefile,我的教授只想输入g ++ <。cpp with main()>来编译..
无论如何这里是我的代码我非常感谢你的帮助!
Asg5.cpp
#include "BinomialTree.h" #include "BinomialNode.h" #include <iostream> #include <vector> #include <fstream> #include <sstream> #include <stdlib.h> #include <string> #include <stdio.h> using namespace std; int main(int argc, char* argv[]) { //input handling if(argc != 2) { cout << "Incorrect Usage. \n Example: ./a.out <filename>" << endl; exit(1); } BinomialTree *tree = new BinomialTree(); char *buffer; char *token; //read file into buffer.************************************** string input; ifstream file; file.open(argv[1]); if(file.is_open()) { string str; while(file.good()) { getline(file,str); input += " " + str; } } else{ cout << "File not found"<< endl; return 1; } file.close(); int buf; stringstream ss(input); vector<int> tokens; while(ss >> buf) { tokens.push_back(buf); } int i = 0; for(i = 0; i < tokens.size(); i++) tree->insert(tokens[i]); //end file reading ******************************************* delete tree; }
BinomialNode.h
#ifndef _BINOMIALNODE_H_ #define _BINOMIALNODE_H_ #include "BinomialTree.h" class BinomialNode { public: int k; BinomialNode *children[20]; int data; BinomialNode(); }; #endif
BinomialNode.cpp
class BinomialNode { BinomialNode::BinomialNode(int n) { this->k = 0; this->data = n; } }
BinomialTree.h
#ifndef _MULTIMAP_H_ #define _MULTIMAP_H_ #include "BinomialNode.h" class BinomialTree { public: BinomialNode * BQ[20]; void insert(int n); void merge(BinomialNode *queue, BinomialNode *in, int k); void print(BinomialNode *root, int tab); }; #endif
BinomialTree.cpp
#include "BinomialNode.h" #include "BinomialTree.h" #include <iostream> #include <cstdlib> class BinomialTree { void BinomialTree::insert(int n) { BinomialNode *in = new BinomialNode(n); if(BQ[0] == NULL) { BQ[0] = in; return; } else merge(BQ[0], in, 0); } void BinomialTree::merge(BinomialNode *queue, BinomialNode *in, int k) { if(queue == NULL) { BQ[k] = in; return; } if(n == NULL) { BQ[k] = queue; return; } if(queue->data > in->data) { merge(in, queue); return; } queue->k++; BinomialNode* temp[queue->k]; int i; for(i = 0; i < queue->k-1; i++) temp[i] = queue->children[i]; temp[queue->k-1] = in; for(i = 0; i < queue->k; i++) queue->children[i] = temp[i]; if(BQ[queue->k] == NULL) { BQ[queue->k] = queue; return; } else merge(queue, BQ[queue->k]); } void BinomialTree::print(BinomialNode *root, int tab) { if(root == NULL) return; int i; for(i = 0; i < tab*5; i++) cout << " "; cout << root->data << endl; for(i = 0; i < root->k; i++) print(root->children[i], tab+1); } }
So when I try to simply compile my code using "g++ Asg5.cpp" I receive the following error
/tmp/cczhpSGO.o: In function 'main':
Asg5.cpp:(.text+0x2fb): undefined reference to 'BinomialTree::insert(int)'
collect2: ld returned 1 exit status
If anyone's wondering why I'm not using a makefile, my professor simply wants to type g++ <.cpp with main()> to compile..
Anyway here's my code I really appreciate the assistance!
Asg5.cpp
#include "BinomialTree.h" #include "BinomialNode.h" #include <iostream> #include <vector> #include <fstream> #include <sstream> #include <stdlib.h> #include <string> #include <stdio.h> using namespace std; int main(int argc, char* argv[]) { //input handling if(argc != 2) { cout << "Incorrect Usage. \n Example: ./a.out <filename>" << endl; exit(1); } BinomialTree *tree = new BinomialTree(); char *buffer; char *token; //read file into buffer.************************************** string input; ifstream file; file.open(argv[1]); if(file.is_open()) { string str; while(file.good()) { getline(file,str); input += " " + str; } } else{ cout << "File not found"<< endl; return 1; } file.close(); int buf; stringstream ss(input); vector<int> tokens; while(ss >> buf) { tokens.push_back(buf); } int i = 0; for(i = 0; i < tokens.size(); i++) tree->insert(tokens[i]); //end file reading ******************************************* delete tree; }
BinomialNode.h
#ifndef _BINOMIALNODE_H_ #define _BINOMIALNODE_H_ #include "BinomialTree.h" class BinomialNode { public: int k; BinomialNode *children[20]; int data; BinomialNode(); }; #endif
BinomialNode.cpp
class BinomialNode { BinomialNode::BinomialNode(int n) { this->k = 0; this->data = n; } }
BinomialTree.h
#ifndef _MULTIMAP_H_ #define _MULTIMAP_H_ #include "BinomialNode.h" class BinomialTree { public: BinomialNode * BQ[20]; void insert(int n); void merge(BinomialNode *queue, BinomialNode *in, int k); void print(BinomialNode *root, int tab); }; #endif
BinomialTree.cpp
#include "BinomialNode.h" #include "BinomialTree.h" #include <iostream> #include <cstdlib> class BinomialTree { void BinomialTree::insert(int n) { BinomialNode *in = new BinomialNode(n); if(BQ[0] == NULL) { BQ[0] = in; return; } else merge(BQ[0], in, 0); } void BinomialTree::merge(BinomialNode *queue, BinomialNode *in, int k) { if(queue == NULL) { BQ[k] = in; return; } if(n == NULL) { BQ[k] = queue; return; } if(queue->data > in->data) { merge(in, queue); return; } queue->k++; BinomialNode* temp[queue->k]; int i; for(i = 0; i < queue->k-1; i++) temp[i] = queue->children[i]; temp[queue->k-1] = in; for(i = 0; i < queue->k; i++) queue->children[i] = temp[i]; if(BQ[queue->k] == NULL) { BQ[queue->k] = queue; return; } else merge(queue, BQ[queue->k]); } void BinomialTree::print(BinomialNode *root, int tab) { if(root == NULL) return; int i; for(i = 0; i < tab*5; i++) cout << " "; cout << root->data << endl; for(i = 0; i < root->k; i++) print(root->children[i], tab+1); } }
原文:https://stackoverflow.com/questions/36783909
最满意答案
感谢Google支持团队提供的代码段,我发现了这一点:
要获得重新洗牌的PCollection:
PCollection<T> reshuffled = data.apply(Repartition.of());
使用的Repartition类:
import com.google.cloud.dataflow.sdk.transforms.DoFn; import com.google.cloud.dataflow.sdk.transforms.GroupByKey; import com.google.cloud.dataflow.sdk.transforms.PTransform; import com.google.cloud.dataflow.sdk.transforms.ParDo; import com.google.cloud.dataflow.sdk.values.KV; import com.google.cloud.dataflow.sdk.values.PCollection; import java.util.concurrent.ThreadLocalRandom; public class Repartition<T> extends PTransform<PCollection<T>, PCollection<T>> { private Repartition() {} public static <T> Repartition<T> of() { return new Repartition<T>(); } @Override public PCollection<T> apply(PCollection<T> input) { return input .apply(ParDo.named("Add arbitrary keys").of(new AddArbitraryKey<T>())) .apply(GroupByKey.<Integer, T>create()) .apply(ParDo.named("Remove arbitrary keys").of(new RemoveArbitraryKey<T>())); } private static class AddArbitraryKey<T> extends DoFn<T, KV<Integer, T>> { @Override public void processElement(ProcessContext c) throws Exception { c.output(KV.of(ThreadLocalRandom.current().nextInt(), c.element())); } } private static class RemoveArbitraryKey<T> extends DoFn<KV<Integer, Iterable<T>>, T> { @Override public void processElement(ProcessContext c) throws Exception { for (T s : c.element().getValue()) { c.output(s); } } } }
Thanks to the code snippet provided by the Google support team I figured it out:
To get a reshuffled PCollection:
PCollection<T> reshuffled = data.apply(Repartition.of());
The Repartition class used:
import com.google.cloud.dataflow.sdk.transforms.DoFn; import com.google.cloud.dataflow.sdk.transforms.GroupByKey; import com.google.cloud.dataflow.sdk.transforms.PTransform; import com.google.cloud.dataflow.sdk.transforms.ParDo; import com.google.cloud.dataflow.sdk.values.KV; import com.google.cloud.dataflow.sdk.values.PCollection; import java.util.concurrent.ThreadLocalRandom; public class Repartition<T> extends PTransform<PCollection<T>, PCollection<T>> { private Repartition() {} public static <T> Repartition<T> of() { return new Repartition<T>(); } @Override public PCollection<T> apply(PCollection<T> input) { return input .apply(ParDo.named("Add arbitrary keys").of(new AddArbitraryKey<T>())) .apply(GroupByKey.<Integer, T>create()) .apply(ParDo.named("Remove arbitrary keys").of(new RemoveArbitraryKey<T>())); } private static class AddArbitraryKey<T> extends DoFn<T, KV<Integer, T>> { @Override public void processElement(ProcessContext c) throws Exception { c.output(KV.of(ThreadLocalRandom.current().nextInt(), c.element())); } } private static class RemoveArbitraryKey<T> extends DoFn<KV<Integer, Iterable<T>>, T> { @Override public void processElement(ProcessContext c) throws Exception { for (T s : c.element().getValue()) { c.output(s); } } } }
相关问答
更多-
如何将PCollection转换为Python数据流中的列表(How I can convert PCollection to a list in python dataflow)[2022-11-20]
对于我的理解你的问题,你想建立给你在P1的ID的SQL语句。 这是你如何实现这一目标的一个例子: sql = """select ID from `table` WHERE ID IN ({})""" with beam.Pipeline(options=StandardOptions()) as p: (p | 'Create' >> beam.Create(['1', '2', '3']) | 'Combine' >> beam.combiners.ToLis ... -
砌体(“重装”)应该可以工作,但你的行中有一个错误$("#container|).masonry("reload");你需要一个正常的报价,而不是像这样的管道: $("#container").masonry("reload");我也认为我不认为你需要同位素来洗牌。最简单的方法是在将瓷砖喂给砖石之前重新排序瓷砖。简单看看我的网站(http:/ /www.phpdevpad.de)。当你点击左边的菜单并尝试不同的组合时,瓷砖会被洗牌。 Masonry("reload") should work but you ...
-
您可以多次从DoFn调用c.output(T) 。 还有一个库变换Flatten.iterables()但在这种情况下你不需要它。 You can call c.output(T) from a DoFn multiple times. There is also a library transform Flatten.iterables() but you don't need it in this case.
-
您发布的代码是执行此操作的正确方法。 确定以预期格式写入接收器时数据占用的大约数量完全是特定于接收器的,Dataflow无法为您执行此操作。 因此,编写一个函数来手动计算它是最好的方法。 请注意,您需要考虑不同的开销来源。 例如,如果你的接收器是一个CSV文件,那么只需添加各个记录字段的长度就可以低估文件占用的字节数。 您需要考虑逗号,空格,换行符,引号,多字节字符等。此开销也完全取决于格式。 但是,如果确保不超过1GB是非常重要的,那么您可以简单地悲观地扩大近似值。 The code you posted ...
-
不幸的是,您目前无法在度量标签上构建带有过滤器的仪表板。 正如您注意到的那样,新的(测试版)Metric Explorer提供了过滤功能,而Stackdriver团队也积极致力于为仪表板图表提供该功能。 如果我收到来自Stackdriver团队的进一步更新或详细信息,我会跟进。 --Andrea Unfortunately, you currently cannot build a dashboard with a filter on a metric label. As you noticed, the ...
-
感谢Google支持团队提供的代码段,我发现了这一点: 要获得重新洗牌的PCollection: PCollection
reshuffled = data.apply(Repartition.of()); 使用的Repartition类: import com.google.cloud.dataflow.sdk.transforms.DoFn; import com.google.cloud.dataflow.sdk.transforms.GroupByKey; import com.google ... -
PCollection本质上是无序的,所以没有“集合中项目的索引”这样的东西 - 但是,你可以在元素本身中包含行号:让PC1成为PCollection
>其中整数是行号 - 基本上是从文本文件中读取行与行号配对。 我们目前没有提供这样做的内置源 - 最好的办法是编写一个简单的DoFn > ,它将文件名作为输入,并使用IOChannelFactory打开文件并读取它逐行排列并用行号发出内容以产生PC1 。 PColle ... -
遗憾的是,Dataflow SDK不会通过Dataflow的BigQueryIO API公开BigQuery返回的模式。 仅Dataflow API中没有“好”的解决方法。 手动定义架构是一种解决方法。 或者,您可以通过jobs: query直接对BigQuery进行单独查询jobs: query在管道构造时jobs: query ,然后将其结果传递给BigQueryIO.Write转换。 这可能会产生额外的成本,但这可以通过稍微改变查询来减少处理的数据量来减轻。 输出的正确性无关紧要,因为您只存储模式。 ...
-
使用Create.empty() 。 由于PCollection是键入的并且需要编码器,因此您还需要指定编码器或类型描述符(即使集合为空),例如PCollection
emptyStrings = Create.of(StringUtf8Coder.of()) 。 Use Create.empty(). Since PCollections are typed and require coders, you'll also need to specify the coder or a typ ... -
如果您想要获取PCollection每个元素的PCollection ,可以使用侧面输入。 请记住,这将从结果中删除所有并行性,并且您的管道可能会变慢。 如果您仍想这样做,那么: side_input_coll = beam.pvalue.AsIterable(my_collection) (p | beam.Create([0]) | beam.FlatMap(lambda _, my_seq: [(elem, i) for i, elem in enumerate(my_seq)], ...