Search code examples
c++charliteralsstring-literalsfunction-call

Type mismatch when returning an int value from an int function with str parameter


I have the following functions:

// Created by onur on 06/06/22.


#include <iostream>
#include <fstream>
#include <opencv2/opencv.hpp>
#include <opencv2/videoio.hpp>
#include "VideoProcessing.h"

VideoProcessing::VideoProcessing() = default;


int VideoProcessing::getFPS(const std::string& video_path) {
    int FPS; //Declaring an integer variable to store the number of total frames//

    cv::VideoCapture cap(video_path); //Declaring an object to capture stream of frames from default camera//
    FPS = cap.get(cv::CAP_PROP_FPS);//Getting the total number of frames//

    cap.release();//Releasing the buffer memory//

    return FPS;
}

unsigned long VideoProcessing::getSize(const std::string& video_path) {
    std::uintmax_t size = std::filesystem::file_size(video_path);
    return size;
}

These functions return int values but they take strings as parameters. This is how I'm using them:

#include <iostream>
#include "src/VideoProcessing.h"

int main() {
    VideoProcessing vid;
    int fps = vid.getFPS('mesh.mp4');
    unsigned long size = vid.getSize('mesh.mp4');

    std::cout << fps << std::endl;
    std::cout << size << std::endl;

    return 0;
}

But there is a red underline under 'mesh.mp4' which reads:

Reference to type 'const std::string' (aka 'const basic_string<char>') could not bind to an rvalue of type 'int'

My return type and the assigned variables match. Where am I going wrong?


Solution

  • You are using a multicharacter literal that has the type int (and conditionally supported) instead of a string literal in these calls

    int fps = vid.getFPS('mesh.mp4');
    unsigned long size = vid.getSize('mesh.mp4');
    

    Instead write

    int fps = vid.getFPS( "mesh.mp4" );
    unsigned long size = vid.getSize( "mesh.mp4" );