我写了一个函数来获取格式的当前日期和时间:DD-MM-YYYY HH:MM:SS.它可以工作,但让我们说,它非常难看.我怎么能做同样的事情,但更简单?
string currentDateToString()
{
time_t now = time(0);
tm *ltm = localtime(&now);
string dateString = "", tmp = "";
tmp = numToString(ltm->tm_mday);
if (tmp.length() == 1)
tmp.insert(0, "0");
dateString += tmp;
dateString += "-";
tmp = numToString(1 + ltm->tm_mon);
if (tmp.length() == 1)
tmp.insert(0, "0");
dateString += tmp;
dateString += "-";
tmp = numToString(1900 + ltm->tm_year);
dateString += tmp;
dateString += " ";
tmp = numToString(ltm->tm_hour);
if (tmp.length() == 1)
tmp.insert(0, "0");
dateString += tmp; …Run Code Online (Sandbox Code Playgroud) 我在几年前参加了一个入门课程后试图重新学习C++,而且我遇到了一些基本的问题.尝试使用友元功能时出现我当前的问题.这是我的2个文件中的代码.
第一:
// fun.cpp
#include <iostream>
using namespace std;
class classA {
friend void funct();
public:
classA(int a=1,int b=2):propa(a),propb(b){cout<<"constructor\n";}
private:
int propa;
int propb;
void outfun(){
cout<<"propa="<<propa<<endl<<"propb="<<propb<<endl;
}
};
void funct(){ // ERROR HERE
cout<<"enter funct"<<endl;
classA tmp(1,2);
tmp.outfun();
cout<<"exit funct"<<endl;
}
Run Code Online (Sandbox Code Playgroud)
第二:
// mainfile.cpp
#include <iostream>
#include "fun.cpp"
using namespace std;
int main(int nargin,char* varargin[]) {
cout<<"call funct"<<endl;
funct();
cout<<"exit main"<<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我得到的错误是"funct()'的多重定义".在将其声明为友元函数时,我使用了错误的语法吗?
如果我有一个本地分支test,而远程分支是test.所以,如果我做了推动,那就是push origin test:test
如何查看我在该分支上执行的本地未提交的提交?
git log?
我只是想制作一些片段,但我不能让它们中的任何一个工作.有人能看出这有什么问题吗?我阅读了他们的文档,并在网上翻阅了一些例子,但它们也没有用.我已经在我的/sublime text 3/packages/user文件夹中找到它并使用约定命名myTest.sublime.snippet.
该片段是:
<snippet>
<content>
<![CDATA[
<!DOCTYPE html lang="en">
<!--[if lt IE 7]><html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]><html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]><html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
<head>
<meta charset="utf-8">
<title>${1:test1|test2|test3} | $2</title>
<meta name="viewport" content="width=device-width">
<meta name="Description" lang="en" content="Description">
<meta name="robots" content="index, follow">
<meta name="robots" content="noodp, noydir">
<!-- Twitter Bootstrap -->
<link href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet">
<link href="//netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.min.css" rel="stylesheet">
<script src="//netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<!--[if lt IE 9]> …Run Code Online (Sandbox Code Playgroud) 我是Boost.Threads的新手,我正在尝试理解如何将函数参数传递给boost::thread_groups::create_thread()函数.在阅读了一些教程和boost文档之后,我明白可以简单地将参数传递给这个函数,但是我无法使这个方法起作用.
我读到的另一个方法是使用函子将参数绑定到我的函数但是会创建参数的副本,我严格要求传递const引用,因为参数将是大矩阵(我计划通过使用boost::cref(Matrix)一次我得到这个简单的例子来工作).
现在,让我们来看看代码:
void printPower(float b, float e)
{
cout<<b<<"\t"<<e<<"\t"<<pow(b,e)<<endl;
boost::this_thread::yield();
return;
}
void thr_main()
{
boost::progress_timer timer;
boost::thread_group threads;
for (float e=0.; e<20.; e++)
{
float b=2.;
threads.create_thread(&printPower,b,e);
}
threads.join_all();
cout << "Threads Done" << endl;
}
Run Code Online (Sandbox Code Playgroud)
这不会编译与以下错误:
mt.cc: In function âvoid thr_main()â:
mt.cc:46: error: no matching function for call to âboost::thread_group::create_thread(void (*)(float, float), float&, float&)â
/usr/local/boost_1_44_0/include/boost/thread/detail/thread.hpp: In member function âvoid boost::detail::thread_data<F>::run() [with F = void (*)(float, float)]â:
mt.cc:55: instantiated from here
/usr/local/boost_1_44_0/include/boost/thread/detail/thread.hpp:61: …Run Code Online (Sandbox Code Playgroud) 正则表达式给我java.lang.IllegalStateException: No match found错误
String requestpattern = "^[A-Za-z]+ \\/+(\\w+)";
Pattern p = Pattern.compile(requestpattern);
Matcher matcher = p.matcher(requeststring);
return matcher.group(1);
Run Code Online (Sandbox Code Playgroud)
请求字符串在哪里
POST //upload/sendData.htm HTTP/1.1
Run Code Online (Sandbox Code Playgroud)
任何帮助,将不胜感激.
我编写了一个简单的C++类示例,其中包含1个非参数构造函数,1个参数构造函数,2个复制构造函数,1个赋值运算符和1个加号运算符.
class Complex {
protected:
float real, img;
public:
Complex () : real(0), img(0) {
cout << "Default constructor\n";
}
Complex (float a, float b) {
cout << "Param constructor" << a << " " << b << endl;
real = a;
img = b;
}
// 2 copy constructors
Complex( const Complex& other ) {
cout << "1st copy constructor " << other.real << " " << other.img << endl;
real = other.real;
img = other.img;
}
Complex( Complex& …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用pygame.midi模块播放声音.这是我使用的代码:
#!/usr/bin/env python
import pygame.midi
import time
pygame.midi.init()
print pygame.midi.get_default_output_id()
print pygame.midi.get_device_info(0)
player = pygame.midi.Output(0)
player.set_instrument(0)
print 'Playing...'
player.note_on(64)
time.sleep(1)
player.note_off(64)
print 'Played'
pygame.midi.quit()
Run Code Online (Sandbox Code Playgroud)
我在搜索例子时发现了类似的代码,这里是输出:
0
('ALSA','Midi Through Port-0',0,1,0)
打...
玩过
PortMidi呼叫失败......
PortMidi:"糟糕的指针"
输入ENTER ...
没有播放声音,我没有找到任何有关Portgidi错误的信息,这个信息在pygame.midi退出后出乎意料地发生.
你有什么主意吗?我正在运行基于debian的linux发行版,如果这可以帮助的话.
我创建了一个自定义TextView,我唯一做的就是在布局xml中放置一个"实例".这会导致应用程序崩溃并出现" Inflate Exception ":
这是完整的自定义类(实际上它还没有"自定义"......):
package com.example.TestApp;
...
public class MyTextView extends TextView {
public MyTextView (Context context) {
super(context);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec); }
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
}
Run Code Online (Sandbox Code Playgroud)
}
完整的布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.example.TestApp.MyTextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello World, MyActivity"
/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
日志是:
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.TestApp/com.example.TestApp.MyActivity}:
android.view.InflateException: Binary XML file line #8: Error inflating class
com.example.TestApp.MyTextView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
at …Run Code Online (Sandbox Code Playgroud) 我怎样才能在std :: vector中存储多个shared_ptr,每个shared_ptr都有一个指向不同类型的指针?
std::vector < ? > vec;
vec.push_back( make_shared<int>(3));
vec.push_back( make_shared<float>(3.14f));
Run Code Online (Sandbox Code Playgroud)
是否有一个基本的多态类可以用于该任务而不必使用特定于编译器的东西?
c++ ×5
android ×1
boost-thread ×1
class ×1
constructor ×1
date ×1
datetime ×1
definition ×1
friend ×1
git ×1
git-svn ×1
java ×1
midi ×1
oop ×1
polymorphism ×1
pygame ×1
python ×1
regex ×1
shared-ptr ×1
string ×1
sublimetext3 ×1
templates ×1
time ×1
vector ×1