小编Cod*_*ash的帖子

以下代码片段中是否有内存泄漏?

我有以下代码片段:

int nWidth = 10;
int *pData = new int[nWidth];
//I do something here
//then I free memory by:
delete pData;
Run Code Online (Sandbox Code Playgroud)

这段代码会产生内存泄漏吗?我必须使用:

delete[] pData;
Run Code Online (Sandbox Code Playgroud)

c++ memory-leaks

2
推荐指数
2
解决办法
201
查看次数

当没有$ _REQUEST时,"if else声明"

我正在创建一个简单的if和else语句来从我的代码所请求的链接中获取值

if($_REQUEST['f_id']=='')
{
    $friend_id=0;
}
else
{
    $friend_id=$_REQUEST['f_id'];
}
Run Code Online (Sandbox Code Playgroud)

并假设链接是 www.example.com/profile.php?f_id=3

现在它很简单,好像f_id是空的或者带有上述if和else语句的值.但是什么是用户正在玩链接,他删除整个?f_id=3链接左边打开www.example.com/profile.php然后如何检测不f_id存在,并在这种情况下重定向到错误页面?

php if-statement request

2
推荐指数
1
解决办法
1万
查看次数

C++函数参数通过值传递

我知道当你以这种方式调用函数时:

foo(MyClass a)
Run Code Online (Sandbox Code Playgroud)

该对象a正在通过值传递,这意味着它正在被复制.

我的问题是它是如何被复制的?说我的类没有复制构造函数,所以如果使用浅复制传递它,那么对象可能会在函数内部发生变化?

c++ copy-constructor

2
推荐指数
2
解决办法
1319
查看次数

C++ ifstream指针打开文件失败

它似乎不像ifstream*->open我预期的那样工作......这是代码:( g++ 4.7使用-std=c++11in 编译MAC OSX 10.7)

#include <string>
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int main(int argc, char** argv)
{
    string line;
    vector<string> fname = {"a.txt","b.txt"};
    vector<ifstream*> files ( 2, new ifstream );

    files[0]->open( fname[0] );
    getline( *files[0], line,  '\n');
    cerr<<"a.txt: "<<line<<endl; 
    //this one prints the first line of a.txt

    line.clear();

    files[1]->open( fname[1] );
    getline( *files[1], line, '\n'); 

    cerr<<"b.txt: "<<line<<endl;
    //but this one fails to print any from b.txt
    //actually, …
Run Code Online (Sandbox Code Playgroud)

c++ ifstream

2
推荐指数
1
解决办法
1666
查看次数

程序总是返回二进制">>":找不到哪个运算符采用了类型为error的左操作数

所以我已经设定了使用以下等式在C++中创建温度转换器的任务:

Celsius = (5/9)*(Fahrenheit – 32)
Run Code Online (Sandbox Code Playgroud)

到目前为止,我已经提出了这个问题(我从一开始就删除了10行评论,所以代码发布在第11行,如果这有任何意义)

#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>

using namespace std;

int main ()
{ 
float celsius;
float farenheit;

std::cout << "**************************" << endl;
std::cout << "*4001COMP-Lab5-Question 1*" << endl;
std::cout << "**************************" << endl << endl;
std::cout << "Please enter a temperature in farenheit: "; 
std::cin >> farenheit >> endl;
std::cout << "Temperature (farenheit): " << endl;
std::cout << "Temperature (celsius): " << celsius << endl;
std::cin.get();
return 0;
}
Run Code Online (Sandbox Code Playgroud)

每当我尝试运行这个程序时,每次都会出现一堆错误:

1> …

c++ temperature

2
推荐指数
1
解决办法
731
查看次数

在C++中使用if/else的奇怪输出

所以我有以下代码:

char command;
cin >> command;
if ( command == 'P' ) {
    do_something();
}
if ( command == 'Q' ) {
    cout << "Exit\n";
    exit(0);
}
else {
    cout << "command= " command << endl; //use for debugging
    cout << "Non-valid input\n";
    exit(1);
}
cout << "exit at completion\n";
exit(0);
}
Run Code Online (Sandbox Code Playgroud)

当我使用输入时P,我的输出do_something()完成后是:

"output from do_something() function"
command= P
Non-valid input
Run Code Online (Sandbox Code Playgroud)

我的问题是为什么我仍然在第一个if语句中调用Non-valid inputafter do_something()?AKA为什么在do_something()完成时仍然会运行?

c++ if-statement

2
推荐指数
1
解决办法
415
查看次数

致命错误LNK1169:找到一个或多个多重定义的符号

我收到此错误:

fatal error LNK1169: one or more multiply defined symbols found
Run Code Online (Sandbox Code Playgroud)

以下是包含代码的两个文件.在文件1中,我有main()函数,我正在调用在名为的第二个文件中写入的函数linklist.cpp.感谢您提前帮助.

文件1 - main.cpp

#include "stdafx.h"
# include "linklist.cpp"


int main(int argc, _TCHAR* argv[])
{
    node *link_list2;
    link_list2 = createList(31);
    addFront(link_list2,33);
    printList(link_list2);
    printf("Hello There Omer Obaid khan\n");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

文件2 - linklist.cpp

# include "stdafx.h"
# include <stdlib.h>
struct node{
    node * next;
    int nodeValue;

};

node* initNode(int number);
node* createList (int value);
void addFront (node *head, int num );
void deleteFront(node*num);
void destroyList(node …
Run Code Online (Sandbox Code Playgroud)

c++ symbols fatal-error

1
推荐指数
1
解决办法
1万
查看次数

Ada声明遇到问题"0范围18..20"

我在使用这个变量声明时遇到了麻烦:

Code_Length at 0 range 18..20;
Run Code Online (Sandbox Code Playgroud)

我熟悉约束,但这at 0是给我适合的东西,我在其他地方找不到任何在线工作示例.

如果我不得不猜测(我完全猜测),at 0将值初始化为0,则在任何后续赋值操作上强制执行约束.但我找不到任何要验证的东西.

ada variable-assignment variable-declaration

1
推荐指数
1
解决办法
328
查看次数

为什么订单很重要?

那为什么当我把cin.clear()那么cin.ignore()程序完美的作品,例如:我把chars和程序没有错误.

但是当我cin.ignore()先放入时cin.clear(),程序不会停止发送错误信号.

这是如何运作的?

不应该删除输入和未fail flag设置?

#include <iostream>

using namespace std;

class time
{
private:
int hours;


public:
void getime()
{
do
   {
   cout << "Enter hours: ";
   cin >> hours;
   if ( hours < 0 || hours > 23 || cin.fail()  )
   {
       cin.clear();
       cin.ignore(10,'\n');
       cerr << "invalid time, minutes must be between 0 and 59 " << endl;


   }
   }while(hours<0 || hours>23);
}
   };

int main()
{ …
Run Code Online (Sandbox Code Playgroud)

c++ ignore clear

1
推荐指数
1
解决办法
131
查看次数

在web.config中指定链接的sql server

如何设置链接服务器MVC3 Web.Config

我们有一个链接服务器,它有一个SP_PROC我们需要运行的存储过程.但是,主SQL服务器TESTSERVER1和链接服务器都是在测试环境中设置的TESTSERVER2,因此名称/ ip将在应用程序上线后更改.因此,我不想SP在代码中指定任何名称以外的内容.

我已经尝试在连接字符串的初始目录字段中指定链接服务器,但是这会抛出错误 - 所以我假设我正在咆哮错误的树.

连接字符串示例:

<add name="connectionName" connectionString="data source=TESTSERVER1;Initial Catalog=TESTSERVER2;user id.../>
Run Code Online (Sandbox Code Playgroud)

c# sql connection-string linked-server

1
推荐指数
1
解决办法
3704
查看次数