当我初始化我的类的对象时,默认和复制构造函数都被调用.
class A
{
public:
A (string s) { str = string (s); cout << "default" << endl; }
A (int n) { cout << "A (int n)" << endl; }
A (string s, int n) { cout << "A (string s, int n)" << endl; }
A (int n2, string s2) { cout << "A (int n2, string s2)" << endl; }
A (const A& a) { str = a.str; cout << "copy" << endl; }
inline void printA …Run Code Online (Sandbox Code Playgroud) 我的目标是允许链接方法,例如:
class Foo;
Foo f;
f.setX(12).setY(90);
Run Code Online (Sandbox Code Playgroud)
是否有可能Foo的方法返回指向其实例的指针,允许这样的链接?
我有一个Flex应用程序(Web,而不是Air)用于大规模分发,可以像各种网站上的插件一样包含在内.
我一直在努力做Ex.10-02在Accelerated C++中,它给了我错误,我最终保持"简化"我的程序,直到我达到这一点,甚至它仍然不会编译(通过g ++)给我错误:
test.cpp: In function ‘int main()’:
test.cpp:22: error: no matching function for call to ‘dumb(__gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >)’
Run Code Online (Sandbox Code Playgroud)
这是我的计划:
#include <algorithm>
#include <iostream>
#include <vector>
using std::cout; using std::endl;
using std::vector;
template <class Ran, class T> T dumb(Ran begin, Ran end)
{
return *begin;
}
int main()
{
vector<int> myVector;
for (int i = 1; i <= 9; ++i)
myVector.push_back(i);
int d = dumb(myVector.begin(), myVector.end());
cout << "Value = " << d …Run Code Online (Sandbox Code Playgroud) 此代码有效:
TcpClient tcpClient = (TcpClient)client;
NetworkStream clientStream = tcpClient.GetStream();
byte[] message = new byte[5242880];
int bytesRead;
bytesRead = clientStream.Read(message, 0, 909699);
Run Code Online (Sandbox Code Playgroud)
但是这会返回错误的字节数:
bytesRead = clientStream.Read(message, 0, 5242880);
Run Code Online (Sandbox Code Playgroud)
为什么?我该如何解决?
(实际数据大小为1475186;代码返回11043作为字节数)
我想对数组中的每个项目进行编号,如下所示:
["hello", "hi", "hey"].number()
> ["1. hello", "2. hi", "3. hey"]
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
Array.prototype.number = function () {
var tempNum = this;
for (i in this) {
tempNum[i] = tempNum[(i + 1)] + ". " + tempNum[i]
}
return tempNum;
}
Run Code Online (Sandbox Code Playgroud)
但这是输出:
["hello", "hi", "hey"].number()
> ["undefined. hello", "undefined. hi", "undefined. hey"]
Run Code Online (Sandbox Code Playgroud)
为什么?我应该如何实现这一点,为什么我的代码不起作用?
来自C,加速C++第5章中的这个例子引起了我的注意:
vector<Student_info> extract_fails(vector<Student_info>& students) {
vector<Student_info> pass, fail;
for (vector<Student_info>::size_type i = 0; i != students.size(); ++i)
if (fgrade(students[i]))
fail.push_back(students[i]);
else
pass.push_back(students[i]);
students = pass;
return fail;
}
Run Code Online (Sandbox Code Playgroud)
因为fail返回,我知道它是一个局部变量不是问题.但为什么pass能够超出当地范围呢?
在"Accelerated C++:Example Programming by Example"中,第6.1.3章是find()来自"algorithm"库的示例:
我们想检查a char是否在字符串中:
bool find_char(char c){
string str = "asdf";
return find(str.begin(), str.end(), c) != str.end();
}
Run Code Online (Sandbox Code Playgroud)
然后,有一个解释find():
它与find_if类似,不同之处在于它不是调用谓词,而是查找作为第三个参数给出的特定值.与find_if一样,如果存在我们想要的值,则该函数返回一个迭代器,表示给定序列中第一次出现的值.如果找不到该值,则find返回其第二个参数.
这让我想知道为什么我们不使用更短更清洁的版本:
bool find_char(char c){
string str = "asdf";
return find(str.begin(), false, c); //does not compile, see Top Answer
}
Run Code Online (Sandbox Code Playgroud)
这被认为是不好的风格?这段代码有问题吗?
Windows将分区标签显示为大写字母(C :).
我需要知道在创建配置文件或硬编码其他路径时要使用的内容(例如在部署脚本中).有关系吗?
我正在使用10个项目的无序列表作为导航栏。使用SSI,我将标题和导航栏放入每个文件中。我想要一种添加class="active"到当前活动页面的规则集中的方法(当前页面对应的规则<li>将具有不同的样式)。
在每个页面中包含文件意味着在包含的文件中,所有项目都不能具有活动类。
仅用几行代码就可以做到这一点吗?(使用jQuery / JS)
我的另一个选择是将URL的最后一部分与href每个列表项中锚点的一部分匹配。
解决方案:(由RomanGorbatko提供)
var tab = window.location.pathname.split("/");
tab = tab[tab.length - 1]; // This probably is not efficient - suggestions?
if (tab != "") $("#nav a#" + tab).addClass("active");
Run Code Online (Sandbox Code Playgroud) 我的问题是关于定义到类中的变量.我告诉你我的问题.
我已经定义了这个类:
class Measure {
int N;
double measure_set[];
char nomefile[];
double T;
public:
void get( );
void printall( );
double mean( );
double thermal_comp( );
};
Run Code Online (Sandbox Code Playgroud)
我想方法得到以下内容:
这就是我所做的:
void Measure::get()
{
cout << "Insert filename:" << endl;
cin >> nomefile;
cout << endl;
cout << nomefile << endl;
cout << endl;
int M=0;
int nmax=50;
ifstream f;
f.open(nomefile);
while(M<nmax)
{
f >> measure_set[M];
if(f.eof())
break;
M++;
}
f.close();
N=M+1;
cout << "Insert temperature:" << endl;
cin >> …Run Code Online (Sandbox Code Playgroud) 我在质疑我对Accelerated C++的最后一个练习的解决方案:
写一个自我复制的程序.这样的程序是没有输入的程序,并且在运行时,在标准输出流上写入其自己的源文本的副本.
我的解决方案
using std::string;
using std::cout;
using std::endl;
using std::ifstream;
using std::getline;
void selfReproduce16_1()
{
ifstream thisFile("C:\\Users\\Kevin\\Documents\\NetBeansProjects\\Accelerated_C++_Exercises\\Chapter_16.cpp", ifstream::in);
string curLine;
bool foundHeader = false;
while(getline(thisFile, curLine))
{
if(!curLine.compare("void selfReproduce16_1()") || foundHeader)
{
foundHeader = true;
cout << curLine << endl;
}
}
}
Run Code Online (Sandbox Code Playgroud)
这仅打印出解决方案的源文本(此功能).这是他们想到的解决方案吗?
我想要一个动态解决方案,不需要硬编码源文件的位置.但是,我不知道在运行时自动获取源文件位置的方法.
与此相关的另一点是包含"包含"文件,并且(当遇到函数调用时),自动获取存储函数的源文件的位置.对我来说,这将是一个真正的"自我复制" "节目.
这在C++中是否可行?如果是这样,怎么样?
我在本教程后面实现了水平视差背景.
我希望垂直具有相同的效果.请帮忙.
到目前为止这是我的代码,
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link rel="stylesheet" type="text/css" href="style.css" />
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script>
var currentX = '';
var movementConstant = .015;
$(document).mousemove(function(e) {
if(currentX == '') currentX = e.pageX;
var xdiff = e.pageX - currentX;
currentX = e.pageX;
$('.parallax div').each(function(i, el) {
var movement = (i + 1) * (xdiff * movementConstant);
var newX = $(el).position().left + movement;
$(el).css('left', newX + 'px');
});
});
</script>
<style>
.parallax {
position: …Run Code Online (Sandbox Code Playgroud) c++ ×7
javascript ×3
iterator ×2
jquery ×2
actionscript ×1
apache-flex ×1
c# ×1
class-design ×1
constructor ×1
css ×1
dynamic ×1
find ×1
flash ×1
hard-drive ×1
navbar ×1
networking ×1
parallax ×1
prototype ×1
quine ×1
reference ×1
runtime ×1
scope ×1
ssi ×1
templates ×1