473,749 Members | 2,636 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

c++ local function definitions are illegal

1 New Member
Hello!
I'm fairly new to c++ but I have been following tutorials and have created functions before, but not one using a string. I can't work out what the problem is here.

The function is supposed to accept a file location, open the file, search for a phrase, and return a result if the certain phrase is found, otherwise return nothing. Then the result is written to a text file later in the program.

Im using visual c++ 2008 and it is currently a console application, which i plan to change once i can get the function working.

Here is my code:

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{

string passtocheckprio rityfunction;
string priorityresult;

string checkpriority (string filenameandpath ){ // my function

string line;

ifstream myinputfile (filenameandpat h);

if (myinputfile.is _open())
{
while (! myinputfile.eof ())
{
getline (myinputfile,li ne);

if (line == "Importance : High"){
return << "Importance : 1.\n";
}
}
myinputfile.clo se();
}
}


cout << "Enter the file path and name";
cin >> passtocheckprio rityfunction;

priorityresult = checkpriority(p asstocheckprior ityfunction)

ofstream myoutputfile ("result.txt ");
myoutputfile << priorityresult;
myoutputfile.cl ose();


return 0;
}


And here is my long list of errors:
------ Build started: Project: FunctionTest, Configuration: Debug Win32 ------
Compiling...
FunctionTest.cp p
c:\documents and settings\me\my documents\visua l studio 2008\projects\f unctiontest\fun ctiontest.cpp(1 7) : error C2601: 'checkpriority' : local function definitions are illegal
c:\documents and settings\me\my documents\visua l studio 2008\projects\f unctiontest\fun ctiontest.cpp(1 2): this line contains a '{' which has not yet been matched
c:\documents and settings\me\my documents\visua l studio 2008\projects\f unctiontest\fun ctiontest.cpp(4 3) : error C2146: syntax error : missing ';' before identifier 'ofstream'
c:\documents and settings\me\my documents\visua l studio 2008\projects\f unctiontest\fun ctiontest.cpp(4 3) : error C2146: syntax error : missing ';' before identifier 'myoutputfile'
c:\documents and settings\me\my documents\visua l studio 2008\projects\f unctiontest\fun ctiontest.cpp(4 3) : error C2275: 'std::ofstream' : illegal use of this type as an expression
c:\program files\microsoft visual studio 9.0\vc\include\ iosfwd(720) : see declaration of 'std::ofstream'
c:\documents and settings\me\my documents\visua l studio 2008\projects\f unctiontest\fun ctiontest.cpp(4 3) : error C3861: 'myoutputfile': identifier not found
c:\documents and settings\me\my documents\visua l studio 2008\projects\f unctiontest\fun ctiontest.cpp(4 4) : error C2065: 'myoutputfile' : undeclared identifier
c:\documents and settings\me\my documents\visua l studio 2008\projects\f unctiontest\fun ctiontest.cpp(4 5) : error C2065: 'myoutputfile' : undeclared identifier
c:\documents and settings\me\my documents\visua l studio 2008\projects\f unctiontest\fun ctiontest.cpp(4 5) : error C2228: left of '.close' must have class/struct/union
type is ''unknown-type''
c:\documents and settings\me\my documents\visua l studio 2008\projects\f unctiontest\fun ctiontest.cpp(2 1) : error C2664: 'std::basic_ifs tream<_Elem,_Tr aits>::basic_if stream(const char *,std::ios_base ::openmode,int) ' : cannot convert parameter 1 from 'std::string' to 'const char *'
with
[
_Elem=char,
_Traits=std::ch ar_traits<char>
]
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
c:\documents and settings\me\my documents\visua l studio 2008\projects\f unctiontest\fun ctiontest.cpp(3 0) : error C2059: syntax error : '<<'
Build log was saved at "file://c:\Documents and Settings\me\My Documents\Visua l Studio 2008\Projects\F unctionTest\Deb ug\BuildLog.htm "
FunctionTest - 10 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Mar 16 '09 #1
3 9188
donbock
2,426 Recognized Expert Top Contributor
Was it your intention to define the checkpriority function right in the middle of the _tmain function? I know you can't do that in C. C++ supports all kinds of magic, so maybe you can do it there.
Mar 16 '09 #2
weaknessforcats
9,208 Recognized Expert Moderator Expert
I see you are using an _tmain() and that tells me you need to support both ASCII and Unicode version of your program.

That means you can't use the C++ string since that is ASCII only. On the Unicode side it is wstring. Ditto for the ASCII cout that is the equivalent of the Unicode wcout, etcc...

You will need a macro:
#ifdef UNICODE
typedef wstring String;
typedef wostream Output; //this replaces wcout
#else
typedef string String;
typedef ostream Output; //this replaces cout
#endif

Also, you cannot use ASCII strings directly:

if (line == "Importance : High"){

but instead need to:

Expand|Select|Wrap|Line Numbers
  1. Output line;
  2. //etc..
  3. if (line == Output("Importance: High")){
Mar 16 '09 #3
MrHenry
2 New Member
Hello, thank you for the reply. I went with tmain() because that is what visual studio automatically gave me. Yes donbock , code wasn't meant to be jumbled like that.

i've worked on my code abit and i've managed to fix the mentioned problem, but i've run into a new problem where my program crashes on "if (line == "Importance : High"){", which I should probably start a new thread for since it's a strange problem.
Mar 16 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
17398
by: Jonathan | last post by:
I'm puzzled by Python's behavior when binding local variables which are introduced within exec() or execfile() statements. First, consider this simple Python program: # main.py def f() : x = 1 print "x:", x f()
2
1526
by: Victor Liu | last post by:
hi, why n1 in local::f() is no allowed ? int n0; void function() { int n1; static int n2; class local {
16
2836
by: Kiuhnm | last post by:
Is there an elegant way to deal with semi-circular definitions? Semi-circular definition: A { B }; B { *A }; Circular reference: A { *B }; B { *A }; The problems arise when there are more semi-circular definitions and
11
1972
by: j23 | last post by:
I have library (static) testlib.cpp: #include <stdarg.h> void xxx(...) { char buf; va_list args; va_start(args, buf); va_end(args); }
23
4017
by: Timothy Madden | last post by:
Hello all. I program C++ since a lot of time now and I still don't know this simple thing: what's the problem with local functions so they are not part of C++ ? There surely are many people who will find them very helpfull. gcc has them as a non-standard option, but only when compiling C language code, so I'm afraid there might be some obscure reason why local functions are not so easy to be dealt with in C++, which I do not yet know.
8
5112
by: Olov Johansson | last post by:
I just found out that JavaScript 1.5 (I tested this with Firefox 1.0.7 and Konqueror 3.5) has support not only for standard function definitions, function expressions (lambdas) and Function constructors (these three I knew about), but also conditional function definitions, as described in http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Guide:Defining_Functions ]. An example: function fun() {
36
3851
by: zouyongbin | last post by:
Stanley B Lippman in his "C++ Primer" that a definition like this should not appear in a header file: int ix; The inclusion of any of these definitions in two or more files of the same program will result in a linker error complaining about multiple definitions. So this kind of definition should be avoided as much as possible. But as we know, the definition of a class is always in a header file. And we can use "#ifndef" to eliminate...
15
25727
by: unknownbomb | last post by:
Hey all i really hope you can help me out. Im compiling a project and im getting 2 errors which are saying local function definitions are illegal. Anyone know what this mean? And especially HOW to fix it?? Thanks.. Sorry if this is a noob question.. I AM still a noobcoder. Greetz
1
4711
by: kaygee | last post by:
Hi ppl can anyone tell me what this error means? error C2601: 'meanData' : local function definitions are illegal
0
8997
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8833
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9568
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9389
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9335
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6801
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6079
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4709
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2794
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.