Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Compile failing on Solaris 10

Reply
Thread Tools

Compile failing on Solaris 10

 
 
Saqib Ali
Guest
Posts: n/a
 
      08-08-2011

I'm on a Solaris 10 box.
The compiler I'm using is: /opt/solstudio12.2/bin/CC

Compiling the file shown below (myTest2.C) fails.

% CC -I. -o myTest2 myTest2.C
"myTest2.C", line 30: Error: "{" expected instead of "myFunc".
"myTest2.C", line 33: Error: "{" expected instead of "myFunc".
2 Error(s) detected.

Why is it an error to call myFunc() while declaring a variable?
How to get around it?

----------------------------------------------------------------------------
#include <iostream>
#include <string>



using namespace std;


char* myFunc(string inString)
{
char outString[1024];
int i;
for (i = 0; i <= inString.size()-1; i++)
outString[i] = inString[i];
outString[i+1] = '\0';
return outString;
}





extern "C" {
}

// This Works:
static char myVariable1 [ ] = "MyString1" ;

// This Breaks:
static char myVariable2 [ ] = myFunc("MyString2") ; // Line #30

// This Breaks:
char myVariable5 [1024] = myFunc("MyString3"); // Line
#33





int main()
{
// This Works:
string Z = myFunc("Gdkkn Vnqkc");
cout << "Z = " << Z << endl << endl;

}
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      08-08-2011
On 8/8/2011 2:41 PM, Saqib Ali wrote:
>
> I'm on a Solaris 10 box.
> The compiler I'm using is: /opt/solstudio12.2/bin/CC


That doesn't really matter. You've broken several rules, and any
compiler worth the energy it takes to run it, should complain.

>
> Compiling the file shown below (myTest2.C) fails.
>
> % CC -I. -o myTest2 myTest2.C
> "myTest2.C", line 30: Error: "{" expected instead of "myFunc".
> "myTest2.C", line 33: Error: "{" expected instead of "myFunc".
> 2 Error(s) detected.
>
> Why is it an error to call myFunc() while declaring a variable?


It's not.

> How to get around it?


It depends on what you're trying to achieve.

>
> ----------------------------------------------------------------------------
> #include<iostream>
> #include<string>
>
>
>
> using namespace std;
>
>
> char* myFunc(string inString)
> {
> char outString[1024];
> int i;
> for (i = 0; i<= inString.size()-1; i++)
> outString[i] = inString[i];
> outString[i+1] = '\0';
> return outString;


You're returning a POINTER to a local array. As soon as the function
returns, the pointer that is returned becomes *invalid*. Don't code
like that.

> }
>
>
>
>
>
> extern "C" {
> }
>
> // This Works:
> static char myVariable1 [ ] = "MyString1" ;


This is explicitly allowed. The array of char of unknown size can be
initialized with a string *literal*.

>
> // This Breaks:
> static char myVariable2 [ ] = myFunc("MyString2") ; // Line #30


An array cannot be initialized from a pointer. Your function call is an
expression that yields a pointer to char. You could write

char* ptr = myFunc("MyString2");
static char myVariable2[] = ptr;

and you'd get the same error.

>
> // This Breaks:
> char myVariable5 [1024] = myFunc("MyString3"); // Line
> #33


Same problem. An attempt to initialize an array from a pointer.

> int main()
> {
> // This Works:
> string Z = myFunc("Gdkkn Vnqkc");


The class 'std::string' has a constructor that takes 'char const*'.
That's called "parameterized constructor" and is explicitly allowed.

What book on C++ are you reading that doesn't explain such basic stuff?

> cout<< "Z = "<< Z<< endl<< endl;
>
> }


V
--
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Ruby compile failing at make ("LIBRUBY_EXTS: command not found") Jonathan Gold Ruby 5 10-26-2010 03:52 AM
Tk Compilation on Solaris 10 Failing krishu Perl Misc 3 12-05-2007 11:06 AM
[cross-posting] perl-gtk installation failing on Solaris kumar Perl Misc 0 03-20-2007 11:37 AM
cant compile on linux system.cant compile on cant compile onlinux system. Nagaraj C++ 1 03-01-2007 11:18 AM
Build failing on Solaris Karl Hanzel Python 2 12-02-2003 09:05 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57