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;

    }

}
  • 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. – Lauri Peltonen Apr 15 '18 at 4:27
  • The code is working fine. Check your version of remix.function is misspelled in getName(). Correct that. – Prashant Prabhakar Singh Apr 15 '18 at 4:36
  • Heh, oops, noob mistake. compiler says its not loaded? – Durian Jaykin Apr 15 '18 at 4:38
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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