Solved

error: list<> does not contain a definition for ' '

Posted on 2012-03-17
1
476 Views
Last Modified: 2012-06-21
Hi all,
I'm getting this error for my following program, can anyone explain the below error and how can i solve it?
thanks in advance.

error CS1061: 'System.Collections.Generic.List<AbstractSyntax.Declaration>' does not contain a definition for 'toStringIndented' and no extension method 'toStringIndented' accepting a first argument of type 'System.Collections.Generic.List<AbstractSyntax.Declaration>' could be found (are you missing a using directive or an assembly reference?)

in my abstractsyntax.cs:
public class CProgram
    {
        List<Declaration> decpart = new List<Declaration>();
        List<Statement> body = new List<Statement>();

        public CProgram(List<Declaration> d, List<Statement> b)
        {
            decpart = d;
            body = b;
        }

        public String toString()
        {
            return toStringIndented("");
        }
        public String toStringIndented(String indent)
        {
            return indent + "Program(\n"
              + decpart.toStringIndented(indent + "  ") + ",\n" <-- error
              + body.toStringIndented(indent + "  ") + "\n" <-- error
              + indent + ")";
        }
    }

public class Declaration
    {
        public List<Declaration> decl = null;

        public Declaration()
        {

        }

        public Declaration(List<Declaration> Declarations)
        {
            this.decl = Declarations;
        }

        public String toString()
        {
            return toStringIndented("");
        }

        public String toStringIndented(String indent)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(indent);
            sb.Append("Declarations(\n");
            int i = 0;
            foreach (Declaration s in decl)
            {
                sb.Append(this.toStringIndented(indent + "  "));
                i++;
                if (i == decl.Count)
                    sb.Append("\n");
                else
                    sb.Append(",\n");
            }
            sb.Append(indent);
            sb.Append(")");
            return sb.ToString();
        }
    }

Open in new window


in my parser.cs:
public void program()
        {
            match(Token.TokenType.Int);
            match(Token.TokenType.Main);
            match(Token.TokenType.LeftParen);
            match(Token.TokenType.RightParen);
            match(Token.TokenType.LeftBrace);
            List<Declaration> decls = declarations();
            List<Statement> stmts = statements();
            match(Token.TokenType.RightBrace);

            if (error_flag == false)
                Console.WriteLine("No Syntax Error. Comgratulation!");

            CProgram p = new CProgram(decls,stmts);
        }

        private List<Declaration> declarations()
        {
            List<Declaration> ds = new List<Declaration>();
            List<Declaration> dsl = new List<Declaration>();
            while (current_token.getType() == Token.TokenType.Int ||
                current_token.getType() == Token.TokenType.Float ||
                current_token.getType() == Token.TokenType.Char ||
                current_token.getType() == Token.TokenType.Bool)
            {
                dsl = declaration();
                ds.AddRange(dsl);
            }
            return ds; 
        }

        private List<Declaration> declaration()
        {
            List<Declaration> result = new List<Declaration>();
            Type t = type();
            Variable v = new Variable(match(Token.TokenType.Identifier));

            if (current_token.getType() == Token.TokenType.LeftBracket)
            {
                match(Token.TokenType.LeftBracket);
                int i = Convert.ToInt32(match(Token.TokenType.IntLiteral));
                match(Token.TokenType.RightBracket);
                ArrayDecl ad = new ArrayDecl(v, t, i);
                result.Add(ad);
            }
            else
            {
                VariableDecl vd = new VariableDecl(v, t);
                result.Add(vd);
            }

            while (current_token.getType() == Token.TokenType.Comma)
            {
                match(Token.TokenType.Comma);
                Variable v1 = new Variable(match(Token.TokenType.Identifier));
                if (current_token.getType() == Token.TokenType.LeftBracket)
                {
                    match(Token.TokenType.LeftBracket);
                    int i = Convert.ToInt32(match(Token.TokenType.IntLiteral));
                    match(Token.TokenType.RightBracket);
                    ArrayDecl ad = new ArrayDecl(v1, t, i);
                    result.Add(ad);
                }
                else
                {
                    VariableDecl vd2 = new VariableDecl(v1, t);
                    result.Add(vd2);
                }
            }

            match(Token.TokenType.Semicolon);

            return result;
        }

Open in new window

0
Comment
Question by:crazy4s
1 Comment
 
LVL 35

Accepted Solution

by:
Miguel Oz earned 500 total points
ID: 37733566
Fields decpart and body are generic list , thus they do not have that method defined.
You need to loop these lists to call the required method, thus in CProgram class change toStringIndented as follows:
public String toStringIndented(String indent)
        {
            string result = indent + "Program(\n"
            foreach(var item in decpart)
                result = result + item.toStringIndented(indent + "  ")  + ",\n";  
            foreach(var item in body)
                result = result + item.toStringIndented(indent + "  ")  + ",\n";  
            return result  + indent + ")";
        }
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now