473,387 Members | 1,534 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

c++ local function definitions are illegal

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 passtocheckpriorityfunction;
string priorityresult;

string checkpriority (string filenameandpath){ // my function

string line;

ifstream myinputfile (filenameandpath);

if (myinputfile.is_open())
{
while (! myinputfile.eof())
{
getline (myinputfile,line);

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


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

priorityresult = checkpriority(passtocheckpriorityfunction)

ofstream myoutputfile ("result.txt");
myoutputfile << priorityresult;
myoutputfile.close();


return 0;
}


And here is my long list of errors:
------ Build started: Project: FunctionTest, Configuration: Debug Win32 ------
Compiling...
FunctionTest.cpp
c:\documents and settings\me\my documents\visual studio 2008\projects\functiontest\functiontest.cpp(17) : error C2601: 'checkpriority' : local function definitions are illegal
c:\documents and settings\me\my documents\visual studio 2008\projects\functiontest\functiontest.cpp(12): this line contains a '{' which has not yet been matched
c:\documents and settings\me\my documents\visual studio 2008\projects\functiontest\functiontest.cpp(43) : error C2146: syntax error : missing ';' before identifier 'ofstream'
c:\documents and settings\me\my documents\visual studio 2008\projects\functiontest\functiontest.cpp(43) : error C2146: syntax error : missing ';' before identifier 'myoutputfile'
c:\documents and settings\me\my documents\visual studio 2008\projects\functiontest\functiontest.cpp(43) : 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\visual studio 2008\projects\functiontest\functiontest.cpp(43) : error C3861: 'myoutputfile': identifier not found
c:\documents and settings\me\my documents\visual studio 2008\projects\functiontest\functiontest.cpp(44) : error C2065: 'myoutputfile' : undeclared identifier
c:\documents and settings\me\my documents\visual studio 2008\projects\functiontest\functiontest.cpp(45) : error C2065: 'myoutputfile' : undeclared identifier
c:\documents and settings\me\my documents\visual studio 2008\projects\functiontest\functiontest.cpp(45) : error C2228: left of '.close' must have class/struct/union
type is ''unknown-type''
c:\documents and settings\me\my documents\visual studio 2008\projects\functiontest\functiontest.cpp(21) : error C2664: 'std::basic_ifstream<_Elem,_Traits>::basic_ifstrea m(const char *,std::ios_base::openmode,int)' : cannot convert parameter 1 from 'std::string' to 'const char *'
with
[
_Elem=char,
_Traits=std::char_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\visual studio 2008\projects\functiontest\functiontest.cpp(30) : error C2059: syntax error : '<<'
Build log was saved at "file://c:\Documents and Settings\me\My Documents\Visual Studio 2008\Projects\FunctionTest\Debug\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 9141
donbock
2,426 Expert 2GB
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 Expert Mod 8TB
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
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
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...
2
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
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...
11
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
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...
8
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...
36
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...
15
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...
1
by: kaygee | last post by:
Hi ppl can anyone tell me what this error means? error C2601: 'meanData' : local function definitions are illegal
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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...

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.