当函数的前向声明在源文件(.cpp)中工作时,为什么同样的不适用于类?
谢谢.
// main.cpp
void forwardDeclaredFunction() ; // This is correct
class One ; // Why this would be wrong
int One:: statVar = 10 ;
void
One :: anyAccess() {
std::cout << "\n statVar:\t " << statVar ;
std::cout << "\n classVar:\t" << classVar ;
}
class One {
public:
void anyAccess() ;
static int statVar ;
private:
int classVar ;
} ;
int main (int argc, char * const argv[]) {
One *obj = new One ;
return …Run Code Online (Sandbox Code Playgroud) 在代码片段中,我能够访问类范围之外的私有成员变量.虽然不应该这样做,为什么在这种情况下允许?通过引用接收返回的私有变量是不好的做法?
#include <iostream>
#include <cstdlib>
class foo
{
int x;
public:
foo(int a):x(a){}
int methodOne() { return x; }
int& methodTwo() { return x; }
};
int main()
{
foo obj(10);
int& x = obj.methodTwo();
x = 20; // With this statement, modifying the state of obj::x
std::cout << obj.methodOne();
getchar();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
关于这种方法,返回类型传达了什么?而且我什么时候应该有这种类型的返回类型?
int& methodTwo() { return x; }
Run Code Online (Sandbox Code Playgroud)
PS:如果主题行含糊不清,我很抱歉.有人可以将其更改为与此处相关的内容.谢谢.
我正在尝试使用AVAudioRecorder的averagePowerForChannel方法来监控iPad/iPhone应用程序的麦克风输入电平.我有一个回调轮询循环中的平均水平 - 在iPhone上它工作正常并返回合理的水平,但由于某种原因在iPad上它总是返回-120.0.
这是我的一些设置代码:
- (void) setupMic {
if (micInput) {
[micInput release];
micInput = nil;
}
NSURL *newURL = [[NSURL alloc] initFileURLWithPath:@"/dev/null"];
NSMutableDictionary *recordSettings = [[NSMutableDictionary alloc] init];
[recordSettings setObject:[NSNumber numberWithInt:kAudioFormatAppleLossless] forKey: AVFormatIDKey];
[recordSettings setObject:[NSNumber numberWithFloat:22050.0] forKey: AVSampleRateKey];
// [recordSettings setObject:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey];
[recordSettings setObject:[NSNumber numberWithInt:12800] forKey:AVEncoderBitRateKey];
[recordSettings setObject:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
[recordSettings setObject:[NSNumber numberWithInt: AVAudioQualityLow] forKey: AVEncoderAudioQualityKey];
micInput = [[AVAudioRecorder alloc] initWithURL:newURL settings:recordSettings error:nil];
// [micInput setMeteringEnabled:YES];
[newURL release];
[recordSettings removeAllObjects];
[recordSettings release];
}
Run Code Online (Sandbox Code Playgroud)
以及我的开始录制方法:
- (void) startRecording { …Run Code Online (Sandbox Code Playgroud) 当在不同的文件中定义结构时,我在尝试使结构正常工作时遇到了一些麻烦.据我所知,错误告诉我结构被定义了两个不同的时间.我相信也许我可能需要在某处使用extern?我尝试过试验并在Google上寻求帮助,但无济于事.
任何帮助都将非常感谢,谢谢.我的所有四个文件都在下面.
文件:Foo.h
typedef struct
{
int number;
} my_struct; // Redefinition; different basic types
Run Code Online (Sandbox Code Playgroud)
文件:Foo.c
#include "Foo.h"
#include "Bar.h"
#include <stdio.h>
my_struct test;
int main(void)
{
test.number = 0;
DoSomething(&test);
printf("Number is: ", &test.number);
}
Run Code Online (Sandbox Code Playgroud)
文件:Bar.h
#include "Foo.h"
void DoSomething(my_struct *number);
Run Code Online (Sandbox Code Playgroud)
文件:Bar.c
#include "Bar.h"
void DoSomething(my_struct *number)
{
number->number = 10;
}
Run Code Online (Sandbox Code Playgroud) 我理解cin.eof()测试流格式.在输入时,输入错误时不会到达字符结尾.我在MSV C++ 2010上对此进行了测试,并没有理解奇怪的结果.无论我给出什么输入,我都会收到程序中存在的格式错误消息.
#include <iostream>
using namespace std;
int main()
{
int i;
cin>> i;
if(!cin.eof())
{
cout<< "\n Format Error \n";
}
else
{
cout<< "\n Correct Input \n";
}
getchar();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
结果我预计:
i =的值
有人可以解释我哪里错了.谢谢.
我正在尝试使用CryptUnprotectData解密WEP配置文件的密钥.我获取配置文件密钥的方法是使用netsh导出配置文件.
netsh wlan export profile name="MyWEP" folder="./"
Run Code Online (Sandbox Code Playgroud)
现在,我手动将密钥材料从netsh命令生成的.xml文件复制到我的程序.顺便说一下,我正在解密的是 -
DATA_BLOB DataOut, DataVerify;
DataOut.cbData = encryptData.length();
DataOut.pbData = (BYTE*)("I_Manually_Copy_The_WEP_Key_Here");
if (CryptUnprotectData( &DataOut,
NULL,
NULL,
NULL,
NULL,
0,
&DataVerify))
{
printf("The decrypted data is: %s\n", DataVerify.pbData);
}
else
{
printf("Failed. Error Code: %d", GetLastError());
}
Run Code Online (Sandbox Code Playgroud)
但我收到错误代码13引用无效数据.我究竟做错了什么 ?在Win 7及更高版本中,我可以直接使用WlanGetProfile和参数WLAN_PROFILE_GET_PLAINTEXT_KEY.但我在Vista上没有选择使用CryptUnprotectData函数.我在这里看过类似的帖子,但是没有得到太多有用的信息.此外,我使用相同的系统具有相同的用户登录凭据.有人可以建议我怎么办?
PS:我在Windows桌面SDK论坛上发布了相同的问题,但还没有得到回复.试试我的运气吧.
我的产品有一个帮助程序可执行文件来卸载所有相关的子产品.我根据所有子产品的升级代码卸载.
首先,我使用MsiEnumRelatedProducts函数从升级代码中获取产品代码.然后我尝试使用MsiConfigureProductEx函数卸载产品.
问题MsiConfigureProductEx是返回错误.
调用函数:MsiConfigureProductsEx
返回代码:1605(0x00000645)
描述:此操作仅对当前安装的产品有效.
为什么MsiEnumRelatedProducts返回无效的产品代码?我搜索了Windows注册表,看看是否存在这样的产品代码.没有.如何调试问题?
编辑:添加了重现问题的最小代码.
// UpgradeCodes is an array having upgrade codes of all modules.
TCHAR lpProductCode[GUID_STR_LENGTH];
const TCHAR tszNoReboot[] = _T("REMOVE=ALL REBOOT=ReallySuppress DISABLE_REBOOT_PROMPT=1");
for (size_t i = 0; i < sizeof(UpgradeCodes) / sizeof(UpgradeCodes[0]); i++)
{
tstring tstrUpgradeCode = UpgradeCodes[i];
DWORD dwIndex = 0;
size_t status;
// for each of the upgrade code, get all the products
do
{
status = MsiEnumRelatedProducts(UpgradeCodes[i],
0,
dwIndex,
lpProductCode);
if (ERROR_SUCCESS == …Run Code Online (Sandbox Code Playgroud) 我用javascript创建了一个计算矢量坐标的程序,一切都很顺利,因为我有正确的公式,但是当我尝试在javascript中使用Math.cos来计算143.1301的余弦时,它从科学计算器返回0.1864而不是0.7999为什么是那?任何人都可以向我解释原因吗?还请给我这个问题的解决方案...在此先感谢... :)这里;我的代码示例
function cyltoxec(a)
{
ans = Math.cos(a);
return ans.toFixed(4);
}
var = x;
return cyltoxec(x);
Run Code Online (Sandbox Code Playgroud) 我试图打印每个角色的地址std::string.但是我不知道内部发生了什么,std::string这导致了这个输出,而对于数组,它给出了我预期的地址.有人可以解释一下发生了什么吗?
#include <iostream>
#include <string>
using namespace std;
int main(){
string str = "Hello";
int a[] = {1,2,3,4,5};
for( int i=0; i<str.length(); ++i )
cout << &str[i] << endl;
cout << "**************" << endl;
for( int i=0; i<5; ++i )
cout << &a[i] << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
Hello
ello
llo
lo
o
**************
0x7fff5fbff950
0x7fff5fbff954
0x7fff5fbff958
0x7fff5fbff95c
0x7fff5fbff960
Run Code Online (Sandbox Code Playgroud) 我想在这里找出这个问题的答案.
首先,
blah[abc] = blah[abc].replaceAll("(.*) (.*)", "$2, $1");
Run Code Online (Sandbox Code Playgroud)
有人可以向我解释一下(.*),$ 2和$ 1是什么?
其次,当我在for语句中嵌套以便反转字符串的两个部分时,我遇到异常错误.我想知道是否有人知道为什么会这样.
谢谢
编辑:这是我收到的错误
线程"main"中的异常java.lang.ArrayIndexOutOfBoundsException:1在ChangeNames.main(ChangeNames.java:21)
c++ ×4
c ×2
visual-c++ ×2
arrays ×1
avfoundation ×1
encryption ×1
extern ×1
function ×1
installer ×1
ios ×1
iostream ×1
java ×1
javascript ×1
regex ×1
replaceall ×1
return-type ×1
string ×1
struct ×1
typedef ×1
wep ×1
winapi ×1
windows ×1
wlanapi ×1