Solved

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

Posted on 2012-03-17
1
488 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 36

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

740 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