Click here to Skip to main content
15,910,471 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Made a GUI with Visual Studio. Trying to pass a string to another cpp file but it's difficult. I have tried extern std::string but it's always empty for the measure.cpp file. Extern int is working fine. Then I tried to make function inside to MyForm.h which could return a string but seems that my knowledge is not enough. Maybe I'm placing the function to wrong place in MyForm.h? Could someone assist me what I might be doing wrong?

This doesn't compile, because in measure.cpp: 'path' identifier not found.

measure.cpp:
C++
using namespace std;
string path3;
string globalPath;
string path1;

int measure() {
path3 = path();
path1 = globalPath; //is empty

}


What I have tried:

MyForm.h:
C++
#pragma once
#include <fstream>
#include <iostream>
#include "measure.h"
#include <string>
#include <msclr\marshal_cppstd.h>

extern int stop; //working
std::string path();
extern std::string globalPath; //not working

namespace Project1 {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;
	using namespace System::IO::Ports;

	

	/// <summary>
	/// Summary for MyForm
	/// </summary>
	public ref class MyForm : public System::Windows::Forms::Form
	{
	public:
		MyForm(void)
		{
			InitializeComponent();
			findPorts();			
		}

		std::string path() {
			return globalPath;
		}

	protected:		
		~MyForm()
.
.
.
Posted
Updated 9-Jan-22 22:47pm
v3

1 solution

In order to call the path() method in your measure.cpp source file, you need to:
  • include form.h in measure.cpp.
  • call it on a instance to the MyForm class (because it is a method, not a standard function).

In order to get better help, you could better detail your requirements.
 
Share this answer
 
Comments
WantToLearn1 10-Jan-22 4:49am    
Actually the main gui file is MyForm.h, corrected into question but it doesnt make any difference.

If I include MyForm.h in measure.h, then it's "identifier not found" for every function in measure.cpp.

I didn't understood your second point, but I will try to google it. Thanks for the keywords.
CPallini 10-Jan-22 4:54am    
In order to use path() you need something like
MyForm myform;
myform.path();
WantToLearn1 10-Jan-22 5:08am    
If I put #include "MyForm.h" to measure.h, then it generates problems for every funtion in measure.cpp, but not if I put in to measure.cpp.

With #include "MyForm.h" in measure cpp and "MyForm myform" at beginning of the file, it gives for that line:
C4430 missing type specifier - int assumed. Note: C++ does not support default-int
C2146 syntax error: missing ';' before identifier 'myform'
C2065 'myform': undeclared identifier

And ; is not really missing.
CPallini 10-Jan-22 5:20am    
replace
MyForm myform;

with
Project1::MyForm myform;

in measure.cpp
WantToLearn1 10-Jan-22 5:29am    
Thanks. Now it gives for that line:

C3145 'myform': global or static variable may not have managed type 'Project1::MyForm'
C2039 '{dtor}': is not a member of 'System::|Disposable'

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900