在图像上覆盖文本的程序会产生无休止的字符串错误

Program to overlay text on an image gives endless string errors

本文关键字:无休止 错误 字符串 程序 图像 覆盖 文本      更新时间:2023-10-16

我写了一个简短的程序来覆盖图像上的文本:

#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void drawMM( const cv::Mat mm, std::string mCap ){
//Input output windows
cv::namedWindow( "Example2_5-out", cv::WINDOW_AUTOSIZE );
//Input window
cv::imshow( "Example2_5-in", mm );
//The Holder of Output(Copyright 2009 Holders Series)
cv::Mat out = mm.clone();
//Smoothing 
cv::putText(out, mCap, cv::Point(40,200), cv::FONT_HERSHEY_PLAIN, 2.0, 255, 2);
//Show the output image.
cv::imshow( "Example2_5-out", out);
//Wait.
cv::waitKey( 0 );     
}
void inStr(int argc, char* argv[]){
if (argc == 1)
{
string st = "If your boyfriend dosn't know what this is.";
cout << st << endl;
}else{
string st = argv[1];
cout << st << endl;
}
}
int main(int argc, char *argv[]) {
string mT << inStr() << endl;
cv::Mat img = cv::imread(argv[1]);
drawMM(img, mT);
return 0;
}

但是无论我做什么,每次尝试编译它时都会收到两个错误:

  1. 意外的令牌:

CMG--00.cpp:39:15: error: expected initialiser before ‘<<’ token string mT << inStr() << endl;

  1. 未声明的范围:

CMG--00.cpp:41:17: error: ‘mT’ was not declared in this scope drawMM(img, mT);

谁能告诉我我做错了什么?

string mT << inStr() << endl;

这条线有几个问题。首先,您不能在此处使用operator<<。如果要创建变量,请使用string mT = inStr();

此外,你的函数inStr有两个参数:argc 和 argv,就像你的主函数一样。您必须将这些参数传递给函数。这会导致string mT = inStr(argc, argv);

下一个问题是您的inStr函数:您正在使用在控制台中打印某些内容的cout而不是返回字符串。您必须调整inStr:将void替换为string并使用return st;而不是当前cout

我建议阅读另一个教程,因为您似乎对该语言缺乏一些基本的了解。