所以我仍然在这里弄清楚C++引用的语义.
我有一个类作为成员的引用,我在构造函数中初始化引用.
template<class T>
class AVLNode {
private:
T & data;
public:
AVLNode(T & newData) {
data = newData;
}
};
Run Code Online (Sandbox Code Playgroud)
但是我在构造函数行上得到了这个错误:
error: uninitialized reference member ‘AVLNode<int>::data’ [-fpermissive]
Run Code Online (Sandbox Code Playgroud)
我没有得到这个,我在构建类时立即初始化引用,所以引用未初始化的问题应该没有问题吗?
我正在尝试ViewPagerIndicator
在我的项目中使用Jake Wharton的库,我按照这里列出的简单教程:
http://viewpagerindicator.com/
但是ClassNotFoundException
当我尝试运行我的项目时,我一直在努力.
这是我在布局中的XML:
<android.support.v4.view.ViewPager
android:id="@+id/day_pager"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<com.viewpagerindicator.TitlePageIndicator
android:id="@+id/titles"
android:layout_height="wrap_content"
android:layout_width="fill_parent" />
Run Code Online (Sandbox Code Playgroud)
这是我拥有的相应代码(dayPage
是一种ViewPager
类型).
ArrayList<Integer> testData = new ArrayList<Integer>();
testData.add(12);
testData.add(13);
testData.add(22);
dayPage = (ViewPager) this.getActivity().findViewById(R.id.day_pager);
dayPage.setAdapter(new DayAdapter(testData));
//Bind the title indicator to the adapter
TitlePageIndicator titleIndicator = (TitlePageIndicator)this.getActivity().findViewById(R.id.titles);
titleIndicator.setViewPager(dayPage);
Run Code Online (Sandbox Code Playgroud)
现在的价值testData
是垃圾.
完整的堆栈跟踪是:
08-25 22:32:23.420: E/AndroidRuntime(1710): FATAL EXCEPTION: main
08-25 22:32:23.420: E/AndroidRuntime(1710): android.view.InflateException: Binary XML file line #12: Error inflating class com.viewpagerindicator.TitlePageIndicator
08-25 22:32:23.420: E/AndroidRuntime(1710): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:581)
08-25 …
Run Code Online (Sandbox Code Playgroud) 我写了一个python脚本来发送电子邮件,但现在我想知道是否可以使用python向Microsoft交换组发送电子邮件?我已经尝试将该组包含在cc和字段中,但这似乎没有成功.它显示,但似乎不对应一组电子邮件; 这只是纯文本.
任何人都知道这是否可行?
我正在使用C++来解析torrent文件的信息哈希,与此站点相比,我无法获得"正确"的哈希值:
我构建了一个非常简单的玩具示例,以确保我有正确的基础知识.
我在sublime中打开了一个.torrent文件并删除了除信息字典之外的所有内容,所以我有一个如下所示的文件:
d6:lengthi729067520e4:name31:ubuntu-12.04.1-desktop-i386.iso12:piece lengthi524288e6:pieces27820:¡´E¶ˆØËš3í ..............(more unreadable stuff.....)..........
Run Code Online (Sandbox Code Playgroud)
我读了这个文件并用这段代码解析它:
#include <string>
#include <sstream>
#include <iomanip>
#include <fstream>
#include <iostream>
#include <openssl/sha.h>
void printHexRep(const unsigned char * test_sha) {
std::cout << "CALLED HEX REP...PREPPING TO PRINT!\n";
std::ostringstream os;
os.fill('0');
os << std::hex;
for (const unsigned char * ptr = test_sha; ptr < test_sha + 20; ptr++) {
os << std::setw(2) << (unsigned int) *ptr;
}
std::cout << os.str() << std::endl << std::endl;
}
int main() {
using namespace std;
ifstream …
Run Code Online (Sandbox Code Playgroud) 嗨,我正在为一门课程实现一个Graph数据结构(图表不是要求的一部分,我选择使用它来解决问题),我的第一个想法是使用邻接列表来实现它,因为这需要更少的内存,我不希望在我的图中有那么多的边.
但后来发生在我身上.我可以使用Map(HashMap
具体)实现邻接列表Graph数据结构.而不是顶点列表,我将有一个顶点Map,然后将一个边的短边列表保存到顶点.
这似乎是我的方式.但是我想知道是否有人可以看到像我这样的学生在使用HashMap
这个时可能错过的任何缺点?(不幸的是我记得在我们过去的时候非常疲惫HashMap
...所以我对它们的了解比我所知道的所有其他数据结构要少.)所以我想确定.
顺便说一下,我正在使用Java.
所以我有一个带有3个actionbar.Tabs的应用程序,它可以在3个片段之间切换.在其中两个片段中,我需要一个ViewPager,它可以在大约6个ListView之间切换.问题是片段显示页面,我试图让它工作,只显示黑色...总是...我已经确认我可以显示其他东西(我试图让我的SherlockFragment成为SherlockListFragment,我可以显示数据).所以问题仅限于我的适配器和我的ViewPager,以及将它们放在适当位置的代码.哦,我还应该提一下,我正在使用ActionBarSherlock,并且(试图)使用ViewPagerIndicator.我已正确导入(最后).
所以这是我的片段的代码:
import vt.finder.schedule.Schedule;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import com.actionbarsherlock.app.SherlockFragment;
import com.viewpagerindicator.TitlePageIndicator;
public class FreeTimeFragment extends SherlockFragment {
//~Data Fields--------------------------------------------
/**
* Adapter used for swiping between days.
*/
private ViewPager dayPage;
/**
* The DayAdapter that is used to switch between course Lists for each day for the listView.
*/
private DayAdapter dayAdapt;
//~Constructors--------------------------------------------
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View view = onCreateView(getLayoutInflater(savedInstanceState), null, savedInstanceState);
LinearLayout layout = (LinearLayout) …
Run Code Online (Sandbox Code Playgroud) android android-fragments android-viewpager actionbarsherlock viewpagerindicator
我在使用C++编写的类时遇到了问题.我刚刚开始学习C++并且我很快就找到了正确的方法,但是在以"正确"的方式进行之后我仍然遇到了错误....所以我提前道歉,如果这只是一些愚蠢的误解(虽然我对此表示怀疑......)相关代码如下
ArrayList.h
#ifndef ARRAYLIST_H_
#define ARRAYLIST_H_
#include <string>
template<class T>
class ArrayList {
private:
//Data fields-------------------//
T *array;
int size;
int capacity;
bool sorted;
//Methods-----------------------//
void reallocate();
void reallocate(int newSize);
T* mergeSort(T* array, int arraySize);
public:
//Constructors------------------//
ArrayList();
ArrayList(int theSize);
//Methods-----------------------//
//Operations
bool add(T element);
bool add(T element, int index);
bool add(ArrayList<T> list);
bool add(ArrayList<T> list, int index);
std:string toString();
};
#endif /* ARRAYLIST_H_ */
Run Code Online (Sandbox Code Playgroud)
ArrayList.cpp
#include "ArrayList.h"
//~Constructors-----------------------------------------------
/**
* Default constructor,
* creates a 20 element ArrayList, of …
Run Code Online (Sandbox Code Playgroud) 所以这是一个概念性的问题.我正在用C++编写一个LinkedList,并且由于Java是我的第一语言,我开始编写我的removeAll函数,以便它只是将尾节点连接到头部(我使用的是Sentinel Nodes btw).但我立即意识到这在C++中不起作用,因为我必须为节点释放内存!
有没有办法绕过整个列表,手动删除每个元素?
嗨我正在使用一个由HashMap支持的Set来跟踪我已经在图表中遍历的边缘.我计划通过添加存储在每个边缘的数据的哈希码的结果来键入该集合.
v.getData().hashCode() + wordV.getData().hashCode()
Run Code Online (Sandbox Code Playgroud)
但是当使用contains来检查边缘是否在集合中时,这有多可靠?我不能假设得到误报吗?无论如何要克服这个?
引起我关注的确切陈述是:
edgeSet.contains(v.getData().hashCode() + wordV.getData().hashCode())
Run Code Online (Sandbox Code Playgroud)
谢谢!
哦顺便说一句,我正在使用Java.
编辑:
我应该在这个问题上明确这一点.在我的图形中没有边缘对象,有顶点对象,每个顶点对象包含更多顶点对象的列表,即边缘.因此,我认为结合您的回答后面的问题是:
我可以使用Set来存储信息的引用而不是对象....?即我可以存储为顶点的数据对象添加两个哈希码的结果吗?
EDIT2:
我确实使用Java库作为我的hashmap,我将其声明如下:
Set<Integer> edgeSet = Collections.newSetFromMap(new ConcurrentHashMap<Integer, Boolean>());
Run Code Online (Sandbox Code Playgroud) 所以在我正在研究的项目中,我收到以下错误:
*** glibc detected *** ./social_network: munmap_chunk(): invalid pointer: 0x09a913b0 ***
Run Code Online (Sandbox Code Playgroud)
一些printf语句显示我释放的结构导致此错误的内存地址为.... 17.那怎么可能呢?怎么可能让它出现......?
这是崩溃前的一些输出:
temp is: 162075592
temp user is 162075408
After free of temp
inside while loop, first line
before free of temp
temp is: 162075760
temp user is 162075480
After free of temp
inside while loop, first line
before free of temp
temp is: 162075568
temp user is 17
Run Code Online (Sandbox Code Playgroud)
这是输出输出的代码
21 void destroy() {
22
23 struct user_node *node;
24
25 struct friend_node *temp;
26 struct friend_node *next_temp; …
Run Code Online (Sandbox Code Playgroud) 所以我的程序开头有一个头文件和2个.c文件.我去编译,我得到错误信息(大量的这些反复)
command_parser.c:74:6: error: static declaration of ‘read_args_file’ follows non-static declaration
command_parser.h:9:6: note: previous declaration of ‘read_args_file’ was here
Run Code Online (Sandbox Code Playgroud)
现在我不在我的程序中使用静态关键字ANYWHERE ...那么为什么GCC会认为我已经声明了一个静态函数?
下面是.h和.c文件中read_args_file声明的相关代码:
void read_args_file(char* file_name, char* out_file_name, int (*command_read)(char* command, FILE* out));
void read_args_file(char* file_name, char* out_file_name, int (*command_read)(char* command, FILE* out)) {
.....
}
Run Code Online (Sandbox Code Playgroud)
编辑:
整个.h文件是:
#ifndef COMMAND_PARSER_H_
#define COMMAND_PARSER_H_
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/* line 8 follows: */
void switch_parsing(int argc, char* argv[], int (*command_read)(char* command, FILE* out), char* (*pr int_usage)()) {
void read_args_file(char* file_name, char* out_file_name, …
Run Code Online (Sandbox Code Playgroud) 所以我有两个类,一个是抽象的,一个不是.
抽象类是Iterator,具体的是LinkedListIterator.两者的代码都在帖子的底部.
我遇到的问题是下面的代码,我在LinkedListIterator中的析构函数的最后一行说得1错误
undefined reference to `Iterator<int>::~Iterator()'
Run Code Online (Sandbox Code Playgroud)
现在我尝试注释掉虚拟~Iterator()析构函数,没有错误,但我得到一个警告说:
Class '[C@800c1' has virtual method 'remove' but non-virtual destructor
Run Code Online (Sandbox Code Playgroud)
所以我的问题是:我是否需要在抽象Iterator基类中使用虚拟析构函数?我已经读过你应该总是有一个,但在这种情况下,LinkedListIterator中的析构函数只是设置值,它不会释放任何东西......
谢谢!
迭代器代码:
template<class T>
class Iterator {
public:
//~Constructors/Destructors------------//
/*
* Destroys necessary resources.
*/
virtual ~Iterator() = 0;
//~Methods-------------------//
/*
* Informs the user whether there are more elements to be iterated
* over in a List.
*
* @return true if there are more elements to iterate over, false otherwise.
*/
virtual bool hasNext() = 0;
/*
* Gets the …
Run Code Online (Sandbox Code Playgroud)