2

enter image description here

Dear all, I am very new to Ethereum, noob-new. This might anger many but, i have to ask, why is pragma solidity not functioning correctly. I have seem countless examples where pragma solidity ^0.4.0; works, but why doesn't it work for me? I tried this out on remix.ethereum.org, the ethereum browser based compiler.

I receive this error, help please, anyone!

browser/Untitled.sol:1:1: ParserError: Expected pragma, import directive or contract/interface/library definition. pragma solidity ^0.4.0;

Here is the code:

pragma solidity ^0.4.0;
contract MyfirstContract{
    string private name;
    uint private age;

    function setName(string newName){
        name=newName;
    }
    fucntion getName() returns (string){
        return;

    }

}
3
  • 1
    You should post your code as a code block so it can be copypasted and tried out. If it's a picture nobody can copy it. My current guess is that you have some strange symbol somewhere there which would become apparent if we could copypaste the code. Apr 15, 2018 at 4:27
  • The code is working fine. Check your version of remix.function is misspelled in getName(). Correct that. Apr 15, 2018 at 4:36
  • Heh, oops, noob mistake. compiler says its not loaded? Apr 15, 2018 at 4:38

3 Answers 3

1

This occurs when compiler has not loaded yet. You should wait for some time

For statement like pragma solidity ^0.4.11, waiting for some time will make that error go away. In this statement, by using ^ we are saying to use any compiler above 0.4.11 so if 0.4.21 is loaded, then also it will work.

But sometimes this error can stay. This happens if you are using specific compiler version, as in the line below:

pragma solidity 0.4.3;

In that case, go to the settings section given on right handside, and select the compiler version which you are using.

2

In case it isn't clear.

pragma "In computer programming, a directive or pragma (from "pragmatic") is a language construct that specifies how a compiler (or other translator) should process its input." - https://en.wikipedia.org/wiki/Directive_(programming)

It's just saying it was designed for a compiler of version 0.4.0 or better (^). The solc compiler is designed to raise an error if it, itself, is not compatible, and this can be a helpful safeguard - possibly preventing an incompatible or untested compiler from being used.

As @Ayushya pointed out, the compiler itself takes a few seconds to load and initialise. In the case of an incompatible compiler, Remix let's the developer choose from a lengthy list of available compilers, so you don't have to settle for the default.

enter image description here

The pragma may be telling you which version of the compiler you need to load to successfully work with a source file. This is potentially important when working with tested code because breaking changes may have been introduced after publication. Words are deprecated from time to time.

Main takeaway: Remix lets you load any compiler you want/need and there are quite a few to choose from. pragma is there to prevent head-scratchers. It suggests which version the developer worked with and presumably tested the code with.

Hope it helps.

0

First of all. I copied and paste exactly what you have posted here in your code. You misspelled "function" on line 9 . After correcting that, it compiles just fine for me. I´m using Remix in Firefox. See if the browser you choose isn´t causing an issue, if you still get an error after correcting the misspelled word. Otherwise, try putting the SPDX License version. Other than that, I would say your issue is the misspelled word. [![compiles fine][2]][2]

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.