小编Sim*_*mon的帖子

C++ - 在返回int的函数中通过引用传递参数

我创建了一个C++程序,以测试通过函数引用传递参数的功能.

#include <iostream>

using namespace std;

int f(int &b) {
    b = b + 1;
    cout << b << endl;
    return b;
}

int main() {
    int t = 10;

    cout << f(t) << " " << t << endl;
    //cout << f(&t) << " " << t << endl;

    system("PAUSE");

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

能否请您解释为什么这个程序不会影响t执行f函数后的值?b传递的参数是引用,所以我认为它的值会在程序执行后发生变化,因为我正在使用main函数中的实际变量,而不是它的副本.在这种情况下,我希望它是11,但它不受程序执行的影响.

为什么会这样?

c++ pass-by-reference

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

OpenCV - 无法使用SurfFeatureDetector类

我正在尝试编译一个使用SURF进行图像匹配的示例openCV项目.

代码列在下面:

#include <stdio.h>
#include <iostream>
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
//#include "opencv2/core/core.hpp"
//#include "opencv2/features2d/features2d.hpp"
//#include "opencv2/highgui/highgui.hpp"

using namespace cv;

void readme();

/** @function main */
int main()
{
/*
if( argc != 3 )
{ readme(); return -1; }

Mat img_1 = imread( argv[1], CV_LOAD_IMAGE_GRAYSCALE );
Mat img_2 = imread( argv[2], CV_LOAD_IMAGE_GRAYSCALE );
*/

Mat img_1 = imread("D:\\A.jpg", CV_LOAD_IMAGE_GRAYSCALE);
Mat img_2 = imread("D:\\backImg.jpg", CV_LOAD_IMAGE_GRAYSCALE);

if( !img_1.data || !img_2.data )
{ std::cout<< " --(!) Error reading images " << …
Run Code Online (Sandbox Code Playgroud)

c++ opencv

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

C++:以特定格式从文件中读取内容

我有一个文件,其中包含以下格式的像素坐标:

234 324
126 345
264 345
Run Code Online (Sandbox Code Playgroud)

我不知道我的文件中有多少对坐标.

如何将它们读入vector<Point>文件?我是在C++中使用阅读函数的初学者.

我试过这个,但它似乎不起作用:

vector<Point> iP, iiP;

ifstream pFile, rFile;
pFile.open("D:\\MATLAB\\WORKSPACE_MATLAB\\pData.txt");
rFile.open("D:\\MATLAB\\WORKSPACE_MATLAB\\rData.txt");

string rBuffer, pBuffer;
Point rPoint, pPoint;

while (getline(pFile, pBuffer))
{
    getline(rFile, rBuffer);

    sscanf(rBuffer.c_str(), "%d %d", rPoint.x, rPoint.y);
    sscanf(pBuffer.c_str(), "%d %d", pPoint.x, pPoint.y);

    iP.push_back(pPoint);
    iiP.push_back(rPoint);
}
Run Code Online (Sandbox Code Playgroud)

我收到一些奇怪的内存错误.难道我做错了什么?如何修复我的代码以便它可以运行?

c++

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

C++:如何使用静态函数定义类

我想创建一个C++类,它只与静态函数合并,无论如何都可以使用.我创建了一个.h包含声明的.cpp文件和一个包含定义的文件.但是,当我在我的代码中使用它时,我收到一些奇怪的错误消息,我不知道如何解决.

这是我的Utils.h文件的内容:

#include <iostream>
#include <fstream>
#include <sstream>

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace std;
using namespace cv;

#include <vector>
#include <opencv/cv.h>
#include <opencv/cxcore.h>

class Utils
{
public:
    static void drawPoint(Mat &img, int R, int G, int B, int x, int y);
};
Run Code Online (Sandbox Code Playgroud)

这是我的Utils.cpp文件的内容:

#include "Utils.h"

void Utils::drawPoint(Mat &img, int R, int G, int B, int x, int y)
{
img.at<Vec3b>(x, y)[0] = R;
img.at<Vec3b>(x, y)[1] = G;
img.at<Vec3b>(x, y)[2] = B; …
Run Code Online (Sandbox Code Playgroud)

c++ opencv static-methods

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

在不使用sqrt函数的情况下查找平方根的底限

假设我有一个整数n,我想找到该数字m的平方小于的最大数字n.

这个问题的最佳解决方案是什么?

c++ algorithm

-4
推荐指数
1
解决办法
4294
查看次数